如何利用python多线程爬取天气网站图片并保存
作者:小生凡一 时间:2022-01-21 18:16:12
目录
1.1 题目
1.2 思路
1.2.1 发送请求
1.2.2 解析网页
1.2.3 获取结点
1.2.4 数据保存 (单线程)
1.2.4 数据保存 (多线程)
总结
1.1 题目
指定一个网站,爬取这个网站中的所有的所有图片,例如中国气象网(www.weather.com.cn),分别使用单线程和多线程的方式爬取。(限定爬取图片数量为学号后3位)
输出信息: 将下载的Url信息在控制台输出,并将下载的图片存储在images子文件中,并给出截图。
1.2 思路
1.2.1 发送请求
构造请求头
import requests,re
import urllib
headers = {
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'zh-CN,zh;q=0.9',
}
url = "http://www.weather.com.cn/"
request = urllib.request.Request(url, headers=headers)
发送请求
request = urllib.request.Request(url, headers=headers)
r = urllib.request.urlopen(request)
1.2.2 解析网页
页面解析,并且替换回车,方便后续进行正则匹配图片。
html = r.read().decode().replace('\n','')
1.2.3 获取结点
使用正则匹配,先获取所有的a标签,然后爬取a标签下面的所有图片
urlList = re.findall('<a href="(.*?)" ',html,re.S)
获取所有的图片
allImageList = []
for k in urlList:
try:
request = urllib.request.Request(k, headers=headers)
r = urllib.request.urlopen(request)
html = r.read().decode().replace('\n','')
imgList = re.findall(r'<img.*?src="(.*?)"', html, re.S)
allImageList+=imgList
except Exception as e:
pass
这里的请求其实也是要用多线程爬取的,所有后续会补上!
1.2.4 数据保存 (单线程)
for i, img in enumerate(allImageList[:102]):
print(f"正在保存第{i + 1}张图片 路径:{img}")
resp = requests.get(img)
with open(f'./image/{img.split("/")[-1]}', 'wb') as f: # 保存到这个image路径下
f.write(resp.content)
1.2.4 数据保存 (多线程)
引入多进程模块
import threading
# 多线程
def download_imgs(imgList,limit):
threads = []
T = [
threading.Thread(target = download, args=(url,i))
for i, url in enumerate(imgList[:limit + 1])
]
for t in T:
t.start()
threads.append(t)
return threads
编写下载函数
def download(img_url,name):
resp = requests.get(img_url)
try:
resp = requests.get(img_url)
with open(f'./images/{name}.jpg', 'wb') as f:
f.write(resp.content)
except Exception as e:
print(f"下载失败: {name} {img_url} -> {e}")
else:
print(f"下载完成: {name} {img_url}")
就很随机
来源:https://juejin.cn/post/7025496982407872519
标签:python,爬虫,天气
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python代码实现备忘录案例讲解
2021-09-15 02:19:15
python使用wxpy实现微信消息防撤回脚本
2023-08-22 21:21:58
网站开发防止中文乱码需要了解的codepage的重要性小结
2011-03-03 11:24:00
pytorch tensorboard可视化的使用详解
2022-09-27 01:01:51
![](https://img.aspxhome.com/file/2023/8/95788_0s.png)
有关缓存 Cache 的随想
2008-06-09 14:25:00
![](https://img.aspxhome.com/file/UploadPic/20086/9/200869143245569s.jpg)
Python图像运算之图像阈值化处理详解
2023-03-20 22:36:24
![](https://img.aspxhome.com/file/2023/5/85205_0s.png)
Golang并发编程之调度器初始化详解
2023-07-13 08:47:11
![](https://img.aspxhome.com/file/2023/5/99635_0s.png)
使用Python第三方库发送电子邮件的示例代码
2021-07-24 11:17:36
![](https://img.aspxhome.com/file/2023/6/97486_0s.jpg)
php设计模式 Singleton(单例模式)
2023-11-20 14:37:50
使用PyQt的QLabel组件实现选定目标框功能的方法示例
2022-01-08 22:43:21
![](https://img.aspxhome.com/file/2023/6/95766_0s.png)
论坛首页效果图设计
2009-03-19 13:46:00
![](https://img.aspxhome.com/file/UploadPic/20093/19/newbbs-600x1523-48s.jpg)
python shutil操作文件实例讲解
2022-05-20 06:42:08
基于Python实现有趣的象棋游戏
2022-06-11 20:37:15
![](https://img.aspxhome.com/file/2023/0/72060_0s.jpg)
python去除空格和换行符的实现方法(推荐)
2021-08-26 02:58:13
浅谈python和C语言混编的几种方式(推荐)
2021-11-30 02:48:51
Python迭代和迭代器详解
2023-11-20 08:52:28
PyCharm安装库numpy失败问题的详细解决方法
2023-05-18 22:45:16
![](https://img.aspxhome.com/file/2023/0/72270_0s.png)
Symfony2之session与cookie用法小结
2023-11-22 02:58:30
pytorch 中的重要模块化接口nn.Module的使用
2023-10-28 10:10:06
关于adfuller函数返回值的参数说明与记录
2023-03-07 13:33:14