鱼C论坛

 找回密码
 立即注册
查看: 1726|回复: 7

[已解决]帮忙看个问题

[复制链接]
发表于 2021-4-6 20:15:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import time
  4. url = "https://www.umei.cc/tupiandaquan/dongwutupian/"
  5. headers = {
  6.     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
  7. }

  8. resp = requests.get(url,headers = headers)
  9. resp.encoding = "utf-8"
  10. # print(resp.text)
  11. main_page = BeautifulSoup(resp.text,"html.parser")
  12. main_list = main_page.find("div",class_="TypeList").find_all("a") #图片一级页面地址
  13. # print(main_list)
  14. for list1 in main_list:
  15.     href = list1.get("href")
  16.     child_resp = requests.get(href)
  17.     child_resp.encoding = "utf-8"
  18.     child_page = BeautifulSoup(child_resp.text,"html.parser")
  19.     child_list = child_page.find("p",align="center").find("img")
  20.     img = child_list.get("src")
  21.     img_name = child_page.get("alt")
  22.     img_resp = requests.get(img)
  23.     img_resp.content
  24.     with open("F:\\img"+img_name,mode="wb") as f:
  25.         f.write(img_resp.content)
  26.         print("over",img_name)
  27.         time.sleep(2)



  28. 运行报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None))  我百度了下说是IP被封了,但是我换了IP还是不行那
复制代码



最佳答案
2021-4-6 20:56:19
2021-04-06_20-54-46 by Sharpstar_kylin.png
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time    : 2021/4/6 20:31
  4. @Author  : 591821661
  5. @File    : Pyhelper.py
  6. @Software: PyCharm
  7. @Version : 0.0.1
  8. @Comments:


  9. """

  10. import requests
  11. import socket
  12. import urllib.request
  13. from bs4 import BeautifulSoup
  14. import time
  15. url = "https://www.umei.cc/tupiandaquan/dongwutupian/"
  16. headers = {
  17.     "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
  18. }

  19. resp = requests.get(url,headers = headers)
  20. resp.encoding = "utf-8"
  21. # print(resp.text)
  22. main_page = BeautifulSoup(resp.text,"html.parser")
  23. main_list = main_page.find("div",class_="TypeList").find_all("a") #图片一级页面地址
  24. # print(main_list)
  25. for list1 in main_list:
  26.     time.sleep(0.2)
  27.     href = list1.get("href")
  28.     child_resp = requests.get(href,headers = headers)
  29.     child_resp.encoding = "utf-8"
  30.     child_page = BeautifulSoup(child_resp.text,"html.parser")
  31.     child_list = child_page.find("p",align="center").find("img")
  32.     img = child_list.get("src")
  33.     img_name = "Picture_Num_%d.jpg" % main_list.index(list1)
  34.     time.sleep(0.2)
  35.     try:
  36.         socket.setdefaulttimeout(15)  # 设置15秒连接超时
  37.         urllib.request.urlretrieve(img, img_name)
  38.     except:
  39.         continue

复制代码


问题解决,原因可能是你没有设置time_out的值 .下载图片超时了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-6 20:25:12 | 显示全部楼层
这种有可能是网的问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 20:28:20 | 显示全部楼层
本帖最后由 591821661 于 2021-4-6 20:29 编辑

这种情况下大概率就是IP问题。我也碰到好多次 使用小飞机~
另外我推荐别使用requests下载图片。。
用urllib.request.urlretrieve(img_url,image_file_name)直接一步到位。
而且后续多线程爬图片也很方便

  1. proxies={
  2.         'http':'127.0.0.1:1080',
  3.         'https':'127.0.0.1:1080'
  4.     }
  5. resp = requests.get(url,headers = headers,proxies= proxies)
复制代码


还有换个Header也是个不错的主意,你可以在看下自己浏览器是什么header 然后复制进去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 20:38:16 | 显示全部楼层
本帖最后由 591821661 于 2021-4-6 20:39 编辑

很奇怪 我也跑出来也是这个问题 应该可以排除是网络问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 20:56:19 | 显示全部楼层    本楼为最佳答案   
2021-04-06_20-54-46 by Sharpstar_kylin.png
  1. # -*- coding: utf-8 -*-
  2. """
  3. @Time    : 2021/4/6 20:31
  4. @Author  : 591821661
  5. @File    : Pyhelper.py
  6. @Software: PyCharm
  7. @Version : 0.0.1
  8. @Comments:


  9. """

  10. import requests
  11. import socket
  12. import urllib.request
  13. from bs4 import BeautifulSoup
  14. import time
  15. url = "https://www.umei.cc/tupiandaquan/dongwutupian/"
  16. headers = {
  17.     "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"
  18. }

  19. resp = requests.get(url,headers = headers)
  20. resp.encoding = "utf-8"
  21. # print(resp.text)
  22. main_page = BeautifulSoup(resp.text,"html.parser")
  23. main_list = main_page.find("div",class_="TypeList").find_all("a") #图片一级页面地址
  24. # print(main_list)
  25. for list1 in main_list:
  26.     time.sleep(0.2)
  27.     href = list1.get("href")
  28.     child_resp = requests.get(href,headers = headers)
  29.     child_resp.encoding = "utf-8"
  30.     child_page = BeautifulSoup(child_resp.text,"html.parser")
  31.     child_list = child_page.find("p",align="center").find("img")
  32.     img = child_list.get("src")
  33.     img_name = "Picture_Num_%d.jpg" % main_list.index(list1)
  34.     time.sleep(0.2)
  35.     try:
  36.         socket.setdefaulttimeout(15)  # 设置15秒连接超时
  37.         urllib.request.urlretrieve(img, img_name)
  38.     except:
  39.         continue

复制代码


问题解决,原因可能是你没有设置time_out的值 .下载图片超时了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-6 21:04:37 | 显示全部楼层
另外补充一句哈  你爬的这种高清图 建议timeout 设为30 不然下载不完。
我这边 能够在15秒内下完的图 30张里面只有12张。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-7 21:52:36 | 显示全部楼层
591821661 发表于 2021-4-6 20:56
问题解决,原因可能是你没有设置time_out的值 .下载图片超时了。

这个程序也是只能下载三张就报错了,和我代码报错的一样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-9 08:40:18 From FishC Mobile | 显示全部楼层
代码小白liu 发表于 2021-4-7 21:52
这个程序也是只能下载三张就报错了,和我代码报错的一样

因为添加了try语句,就算报错也会继续运行。
如果需要把所有图片都下载下来,可以把延时设为30s,但这也不能保证都下载,和你本机的网络环境有关。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-19 02:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表