数据采集(一):requests爬取图片(3种方式)
来源:互联网 发布:java垃圾回收机制 英文 编辑:程序博客网 时间:2024/05/18 12:42
举例爬取百度贴吧上一张网页上的图片,附上相关html源码,网址失效也无关系,重在分析学习。
<div id="post_content_87286618651" class="d_post_content j_d_post_content clearfix"> <img class="BDE_Image" src="http://imgsrc.baidu.com/forum/w%3D580/sign=b2310eb7be389b5038ffe05ab534e5f1/680c676d55fbb2fbc7f64cbb484a20a44423dc98.jpg" size="21406" changedsize="true" width="560" height="747" style="cursor: url("http://tb2.bdstatic.com/tb/static-pb/img/cur_zin.cur"), pointer;"></div>
首先…
打开网页
# -*- coding: utf-8 -*-import requestsurl = 'http://tieba.baidu.com/p/4468445702'html = requests.get(url)#指定编码html.encoding='utf-8'
然后…
获取url (3种方式)
使用 BeautifulSoup 库
from bs4 import BeautifulSoupbs = BeautifulSoup(html.content,'html.parser')img_list = bs.find('div',{'id':'post_content_87286618651'}).findAll('img')img_src = img_list[0].attrs['src']print(img_src)
使用xpath
from lxml import etreeselector = etree.HTML(html.content)images = selector.xpath('//*[@id="post_content_87286618651"]/img')print image.attrib.get('src')
使用正则表达式
import retext = html.contentpattern = re.compile(r'<img .*src="(.*?)" size="21406"',re.S)match = pattern.search(text)print match.group(1)
最后…
将图像写入文件
img = requests.get(img_src)with open('baidu_tieba.jpg', 'ab') as f: f.write(img.content) f.close()
阅读全文
0 0
- 数据采集(一):requests爬取图片(3种方式)
- 利用Requests爬取图片与网页数据
- python +requests 实现爬取百度图片
- 用 requests-bs4 爬取网络图片
- 用requests爬取百度搜索数据
- Python requests爬虫爬取小说数据
- Python数据采集之Requests
- 数据采集(五):用requests模拟登陆豆瓣
- 数据采集(三):用XPath爬取腾讯新闻
- 爬虫:用requests和BeautifulSoup爬取网上图片
- Python网络爬虫requests、bs4爬取空姐网图片
- python3 requests库实现多图片爬取
- 【Python爬虫系列】使用requests爬取图片
- python:使用requests,bs4爬取mmjpg上的图片
- requests爬取小说
- 利用bs4和requests爬取股票历史交易数据
- 用 Python requests库 爬取网页数据
- 使用Requests+xpath实现简单的数据爬取
- abstract class和interface的区别
- python基础补充部分
- 面试技术要点java
- css盒子模型
- Linux学习之路(1)
- 数据采集(一):requests爬取图片(3种方式)
- C++ Primer 初学心得(壹)
- 嵌入式入门计划
- HDU 2037
- 两个单链表相交的一系列问题
- C++学习笔记(一)
- 使用wampserver搭建阿里云服务器
- 如何搜索二叉树转换为双向链表
- 一个div中文字居中的方法