python爬虫项目-一见倾心壁纸
2024-09-01 17:13:30
目录
方法1
import re
import os
import urllib
import urllib.request def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
return html def getImage(html,x):
#https://mmbiz.qpic.cn/mmbiz_jpg/ib55rg6wzUc3B16KIY3uU53nkcTTDic8uEA4WWBPaHJ8LpibvAnkpS2FZtyjrv7w7dbEeNrhfvPuuyReNAxsLdgJA/640?wx_fmt=jpeg
#https://mmbiz.qpic.cn/mmbiz_jpg/ib55rg6wzUc3B16KIY3uU53nkcTTDic8uEHqocI7r86nehl2NeForAqvcTiaEAIuWjTWPKNXnnXIPuUuqnuJeFKYw/640?wx_fmt=jpeg
#此处正则为重点
reg = 'data-src="(.*?)"'
image = re.compile(reg)
imlist = re.findall(reg,html.decode('utf-8')) print(imlist)
for i in imlist:
print(i)
print(x)
# 下载内容与.py一起
#urllib.request.urlretrieve(i,'%s.jpg' % x)
# 下载位置自定义
urllib.request.urlretrieve(i, r'F:\desktop\爬取的图片\%s.jpg' % x)
x +=1
return x
# -------------递归创建的目录-----------
path = r"F:\desktop\爬取的图片"
if not os.path.exists(path):
os.makedirs(path)
#-----------存储目录创建结束------------
# main 下载结果与此.py文件在同一目录'
x=1
url = 'https://mp.weixin.qq.com/s/MVDcn0O3093OlIhMYkqBIA'
html = getHtml(url)
x = getImage(html,x)
print('下载完成')
方法2:BeautifulSoup 避免写正则表达式(因为不会)
import os
import requests
import urllib.request
from bs4 import BeautifulSoup
#微信下载图片的网址
url = "https://mp.weixin.qq.com/s/cm3Bua0UM1jbZnr2de7TWg"
r = requests.get(url)
demo = r.text
soup = BeautifulSoup(demo, "html.parser")
piclist = []
for link in soup.find_all('img'):
link_list = link.get('data-src')
if link_list != None:
piclist.append(link_list)
# print(piclist)
# print(type(link_list))
# -------------递归创建的目录-----------
path = r"F:\desktop\爬取的图片"
if not os.path.exists(path):
os.makedirs(path)
#-----------存储目录创建结束------------
x = 0
for http in piclist:
print(http)
filesavepath = r'F:\desktop\爬取的图片\%s.jpg' % x
urllib.request.urlretrieve(http, filesavepath)
x += 1
print('正在保存第{:.0f}张图片'.format(x))
print('下载完成')
最新文章
- curl -w,–write-out参数详解
- github
- [转]Installing python 2.7 on centos 6.3. Follow this sequence exactly for centos machine only
- 【HEVC】4、HM-16.7编码一个CU(帧内部分) 3.帧内预测各种模式实现
- NGINX: 405 Not Allowed
- G面经prepare: Maximum Subsequence in Another String's Order
- 自动编号维护SNRO
- Android生命周期和Service生命周期
- bzoj 2037: [Sdoi2008]Sue的小球
- cygwin chmod 失效
- JSP之错误信息提示
- 关于黑名单IP的设置
- WireShark过滤语法
- 斐波那契数列 的两种实现方式(Java)
- lang
- 【憩园】C#并发编程之异步编程(二)
- mysql 新建用户并赋予远程访问权限
- springboot全局捕获异常
- GBDT 算法:原理篇
- shell丢弃信息
热门文章
- 《TypeScript入门教程》笔记
- Mysql常用数据类型归纳总结1
- [2018-06-28] 创建第一个django项目
- [考试反思]1001csp-s模拟测试(b):逃离
- 使用Typescript重构axios(三十一)——添加axios.all和axios.spread方法
- CSS(4)---三大特性(继承性,层叠性,优先级)
- jquery鼠标点击穿透的解决方法
- Python面向对象 | 类的成员
- maven的项目结构
- kubernetes的ingress-nginx