m0_73537734 2022-09-28 20:27 采纳率: 100%
浏览 14
已结题

python爬虫疑问

问题遇到的现象和发生背景

爬虫中 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 循环中的状态码和单独请求一致

  • 写回答

1条回答 默认 最新

  • honestman_ 2022-09-29 00:37
    关注

    因为有的url 比如https://wx3.sinaimg.cn/large/005tGGVqgy1h6hjo33dy1j323i72qb2e.jpg 后面多了一个空格,把空格去掉就行了,解决问题的话帮忙点击以下采纳谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月7日
  • 已采纳回答 9月29日
  • 创建了问题 9月28日

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看