python 爬图片变成404

import os.path

from re import findall
from urllib.request import urlopen
from urllib.request import Request




path='D:/python/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
if not os.path.exists(path):
    os.mkdir(path)

url = 'http://www.27270.com/tag/234.html'

req = Request(url=url, headers=headers)
with urlopen(req) as fp:
    content = fp.read().decode('gbk')

pattern = 'src="(.+?)" width'
result = findall(pattern, content)

for index, item in enumerate(result):
    itemreq = Request(url=str(item), headers=headers)
    with urlopen(itemreq) as fp:
        with open(path+str(index)+'.jpg','wb') as f:
            f.write(fp.read())

print('完成')

图片描述

阅读 5k
2 个回答

两种可能,1是那图片真的是不存在。更大的可能是反爬虫,比如说referer的http头的判断,来拦截。解决的方式,用chrome或firefox,把实际请求的http头弄下来,然后再去请求。另外,做爬虫,可以用requests这个库,简单好用。

header里加上referer就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题