|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我使用小甲鱼的零基础入门学习Python,用python3.6.5实现书本上188页的代码,爬取贴吧图片,出现了以下报错,求帮助 ,最后出现了报错:urllib.error.URLError: <urlopen error unknown url type: img class="bde_image" src="https>
代码如下
def open_url(url):
req=urllib.request.Request(url)
req.add_header('User_Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36 LBBROWSER')
page=urllib.request.urlopen(req)
html=page.read().decode('utf-8')
return html
def get_img(html):
p=r'<img class="BDE_Image".*?src="[^"]*\.jpg".*?>'
imglist=re.findall(p,html)
try:
os.mkdir('d:\hello')
except FileExistsError:
pass
os.chdir('d:\hello')
for each in imglist:
filename=each.split("/")[-1]
urllib.request.urlretrieve(each,filename,None)
if __name__=='__main__':
url="https://tieba.baidu.com/p/3823765471"
get_img(open_url(url)) |
|