问题遇到的现象和发生背景
爬虫中 for 循环遍历图片地址列表时,其中地址的状态码有 404 也有 200,但是我单独 get 请求循环中状态码为 404 的地址时,状态码又是 200 。
用代码块功能插入代码,请勿粘贴截图
#url地址
url = 'https://dimtown.com/62526.html'
#伪装 请求头 防止被反爬
headers = {
#浏览器基本身份信息
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Mobile Safari/537.36 Edg/105.0.1343.42'
}
form_data = {
'log': ' ',
'pwd': ' ',
'redirect_to': 'https://dimtown.com/',
'ludou_token': '75e4ab8f52731de18ca618c12fa96256',
'wp-submit': ''
}
#创建自定义变量 response post 请求方法,对 url 地址发送请求,并携带 headers 请求头伪装
response = requests.post(url = url,headers = headers,data = form_data)
#查看状态码
print(response)
#获取数据
#print(response.text)
#转换数据 转成可以解析对象()
selector = parsel.Selector(response.text)
#print(selector)
#查找图片 url
img_url_list = selector.css('p img::attr(src)').getall()
print(img_url_list)
for img_url in img_url_list:
print(img_url)
img = requests.get(url = img_url, headers = headers)
print(img)
img = requests.get('https://wx4.sinaimg.cn/large/005tGGVqgy1h6hjnkyp81j31qbcn24qw.jpg', headers = headers)
print(img)
运行结果及报错内容
for 循环结果为:
https://wx4.sinaimg.cn/large/005tGGVqgy1h6hjnkyp81j31qbcn24qw.jpg
<Response [404]>
https://wx3.sinaimg.cn/large/005tGGVqgy1h6hjnrd6mdj320ucmyb2j.jpg
<Response [404]>
https://wx4.sinaimg.cn/large/005tGGVqgy1h6hjnylvy6j32q5c5e1l5.jpg
<Response [404]>
https://wx3.sinaimg.cn/large/005tGGVqgy1h6hjo33dy1j323i72qb2e.jpg
<Response [404]>
https://wx1.sinaimg.cn/mw2000/005tGGVqgy1h6hjon5uboj343c64wu12.jpg
<Response [200]>
https://wx1.sinaimg.cn/mw2000/005tGGVqgy1h6hjods3ogj364w43cx6v.jpg
<Response [200]>
https://wx2.sinaimg.cn/mw2000/005tGGVqgy1h6hjoulxebj364w43cnph.jpg
<Response [200]>
https://wx2.sinaimg.cn/mw2000/005tGGVqgy1h6hjoipbqlj343c64w7wl.jpg
<Response [200]>
https://wx4.sinaimg.cn/mw2000/005tGGVqgy1h6hjo8asp2j343c64whe2.jpg
<Response [200]>
单独 get 请求结果:
<Response [200]>
我想要达到的结果
for 循环中的状态码和单独请求一致