目录

一、利用正则表达式

二、利用beautifulsoup


方法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('下载完成')

最新文章

  1. curl -w,–write-out参数详解
  2. github
  3. [转]Installing python 2.7 on centos 6.3. Follow this sequence exactly for centos machine only
  4. 【HEVC】4、HM-16.7编码一个CU(帧内部分) 3.帧内预测各种模式实现
  5. NGINX: 405 Not Allowed
  6. G面经prepare: Maximum Subsequence in Another String's Order
  7. 自动编号维护SNRO
  8. Android生命周期和Service生命周期
  9. bzoj 2037: [Sdoi2008]Sue的小球
  10. cygwin chmod 失效
  11. JSP之错误信息提示
  12. 关于黑名单IP的设置
  13. WireShark过滤语法
  14. 斐波那契数列 的两种实现方式(Java)
  15. lang
  16. 【憩园】C#并发编程之异步编程(二)
  17. mysql 新建用户并赋予远程访问权限
  18. springboot全局捕获异常
  19. GBDT 算法:原理篇
  20. shell丢弃信息

热门文章

  1. 《TypeScript入门教程》笔记
  2. Mysql常用数据类型归纳总结1
  3. [2018-06-28] 创建第一个django项目
  4. [考试反思]1001csp-s模拟测试(b):逃离
  5. 使用Typescript重构axios(三十一)——添加axios.all和axios.spread方法
  6. CSS(4)---三大特性(继承性,层叠性,优先级)
  7. jquery鼠标点击穿透的解决方法
  8. Python面向对象 | 类的成员
  9. maven的项目结构
  10. kubernetes的ingress-nginx