|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import requests
- from bs4 import BeautifulSoup
- import time
- url = "https://www.umei.cc/tupiandaquan/dongwutupian/"
- headers = {
- "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"
- }
- resp = requests.get(url,headers = headers)
- resp.encoding = "utf-8"
- # print(resp.text)
- main_page = BeautifulSoup(resp.text,"html.parser")
- main_list = main_page.find("div",class_="TypeList").find_all("a") #图片一级页面地址
- # print(main_list)
- for list1 in main_list:
- href = list1.get("href")
- child_resp = requests.get(href)
- child_resp.encoding = "utf-8"
- child_page = BeautifulSoup(child_resp.text,"html.parser")
- child_list = child_page.find("p",align="center").find("img")
- img = child_list.get("src")
- img_name = child_page.get("alt")
- img_resp = requests.get(img)
- img_resp.content
- with open("F:\\img"+img_name,mode="wb") as f:
- f.write(img_resp.content)
- print("over",img_name)
- time.sleep(2)
- 运行报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, '远程主机强迫关闭了一个现有的连接。', None, 10054, None)) 我百度了下说是IP被封了,但是我换了IP还是不行那
复制代码
- # -*- coding: utf-8 -*-
- """
- @Time : 2021/4/6 20:31
- @Author : 591821661
- @File : Pyhelper.py
- @Software: PyCharm
- @Version : 0.0.1
- @Comments:
- """
- import requests
- import socket
- import urllib.request
- from bs4 import BeautifulSoup
- import time
- url = "https://www.umei.cc/tupiandaquan/dongwutupian/"
- headers = {
- "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"
- }
- resp = requests.get(url,headers = headers)
- resp.encoding = "utf-8"
- # print(resp.text)
- main_page = BeautifulSoup(resp.text,"html.parser")
- main_list = main_page.find("div",class_="TypeList").find_all("a") #图片一级页面地址
- # print(main_list)
- for list1 in main_list:
- time.sleep(0.2)
- href = list1.get("href")
- child_resp = requests.get(href,headers = headers)
- child_resp.encoding = "utf-8"
- child_page = BeautifulSoup(child_resp.text,"html.parser")
- child_list = child_page.find("p",align="center").find("img")
- img = child_list.get("src")
- img_name = "Picture_Num_%d.jpg" % main_list.index(list1)
- time.sleep(0.2)
- try:
- socket.setdefaulttimeout(15) # 设置15秒连接超时
- urllib.request.urlretrieve(img, img_name)
- except:
- continue
复制代码
问题解决,原因可能是你没有设置time_out的值 .下载图片超时了。
|
|