利用Python网络爬虫抓取豆瓣首页图片代码分享
来源:互联网 发布:淘宝api查询商品 编辑:程序博客网 时间:2024/05/12 10:13
# 爬取豆瓣上的图片import urllib.requestimport sslimport re#定义一个能够从html文件里面利用正则表达式抓取出图片地址的函数def getImgLinks(html): imgRe = re.compile(r'<img.*?(http[\w/:\.-]+?\.jpg).*?') imgLinks = imgRe.findall(html) print(list(imgLinks)) return imgLinks#打开具体的网址,并抓取其html页面 context = ssl._create_unverified_context()weburl = "https://www.douban.com/"#要爬取图片所在的网址# weburl = "https://www.sina.com.cn/"webheader = { 'Accept': 'text/html, application/xhtml+xml, */*', 'Accept-Language': 'zh-CN', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko', 'DNT': '1', 'Connection': 'Keep-Alive', }req = urllib.request.Request(url=weburl,headers=webheader)#构造请求报头webPage = urllib.request.urlopen(req,context=context)data = webPage.read().decode('utf-8')print("抓取网页成功")#主函数,依次捕获图片imgLinks = getImgLinks(data)print(len(imgLinks))count = 0localStorePath = "D:/Python学习"for imgLink in imgLinks: count += 1 print('现在开始抓取第{}个图片,其地址是{}'.format(count,imgLink)) try: #以数字序号命名图片的两种方式 # urllib.request.urlretrieve(imgLink, localStorePath + '/%s.jpg'%count) # urllib.request.urlretrieve(imgLink, localStorePath + '/{}.jpg'.format(count)) #以图片本身的名字命名 print(re.findall(r'([\w-]+)\.jpg',imgLink)[0]) urllib.request.urlretrieve(imgLink, localStorePath + '/{}.jpg'.format(re.findall(r'([\w-]+)\.jpg',imgLink)[0]))#以地址为文件名保存 except: print('抓取第{}个时出现问题'.format(count)) continue 这是我自己写的代码,有两个方面供大家和大家分享,一是抓取html页面的时候规避ssl证书错误,二是保存图片时命名的方式。
阅读全文
0 0
- 利用Python网络爬虫抓取豆瓣首页图片代码分享
- python网络爬虫抓取图片
- python 网络爬虫抓取图片
- python网络爬虫抓取图片
- python网络爬虫抓取图片
- python网络爬虫抓取图片
- 利用Python抓取网络图片
- python网络爬虫,抓取网页图片
- Python爬虫之抓取网络图片
- python网络爬虫(1)--抓取图片
- 【初学Python】利用python抓取bing首页图片
- 爬虫抓取豆瓣小组里的图片
- Python 爬虫 抓取豆瓣读书TOP250
- python爬虫抓取图片
- 网络爬虫--python抓取豆瓣同城北京地区活动信息
- Python实例:网络爬虫抓取豆瓣3万本书(1)
- Python实例:网络爬虫抓取豆瓣3万本书(2)
- Python实例:网络爬虫抓取豆瓣3万本书(3)
- linux format 进度条
- 机器学习算法(一)线性回归
- 美团点评2016研发题目-[编程题-美团] 字符编码
- HDU
- ACM模板 字符串
- 利用Python网络爬虫抓取豆瓣首页图片代码分享
- POJ 魔兽世界之一:备战 题解
- 一些小细节让你的Java程序性能大幅提高
- acm省赛题--J company
- 基础控件4_1 View的基本事件
- HDU 1874畅通工程续
- [Scala]Scala学习笔记一 基础
- 设计模式(14) 策略模式(简单入门 行为模式)
- PAT 乙级 1030. 完美数列(25)