python爬数据小试牛刀--beautifulSoup使用
来源:互联网 发布:工业控制网络视频 编辑:程序博客网 时间:2024/06/15 04:06
python爬数据小试牛刀–beautifulSoup使用
1.环境配置
- 编译环境:python 2.7
- 编译器:pycharm
- HTML或XML提取工具:beautifulSoup(安装自行百度)
2.网站分析
- 网站:斗鱼(http://www.douyu.com)
- 爬取目标:首页的图片
- 步骤一:查看图片信息,鼠标右键图片,选择检查
-
- 步骤二:分析发现图片连接都在src下面
- 步骤三:代码编写
- 导入库
import urllib from bs4 import BeautifulSoup
- 获取网页
import urllibfrom bs4 import BeautifulSoupf=urllib.urlopen("http://www.douyu.com")html =f.read()soup = BeautifulSoup(html, 'html.parser')
- 匹配查询
ss=soup.find_all('img')print sslenth=int(len(ss))print lenthfor i in range(lenth): url =ss[i].attrs['src'] print url tad=url.rfind('.') print tad if tad>0: str= url[tad+1:tad+4] if str=='png': print "this is png" urllib.urlretrieve(url, './img2/png%d.png'%i) elif str=='jpg': print 'this is jpg' urllib.urlretrieve(url, './img2/img%d.jpg' % i) elif str=='gif': print "this is gif" urllib.urlretrieve(url, './img2/gif%d.gif' % i) else:print "Error"
3.总结
获取图片的过程中,发现图片有jpg,png,和gif,于是通过字符串操作,把格式区分开来。
阅读全文
0 0
- python爬数据小试牛刀--beautifulSoup使用
- Python 使用BeautifulSoup方式爬虫爬取数据
- python 中使用BeautifulSoup
- Python BeautifulSoup基本使用
- 【Python】BeautifulSoup使用
- Python-beautifulsoup使用
- Python数据采集1-BeautifulSoup
- Python网络数据采集-BeautifulSoup
- Python数据采集之BeautifulSoup
- Python爬虫:用BeautifulSoup进行NBA数据爬取
- [python爬虫] BeautifulSoup爬取+CSV存储贵州农产品数据
- python selenium+beautifulSoup爬取彩票网数据
- Python使用BeautifulSoup进行爬虫
- python中BeautifulSoup简单使用
- python网络爬虫-使用BeautifulSoup
- Python爬取淘宝搜索页,使用Selenium+BeautifulSoup
- python学习笔记 BeautifulSoup趴数据
- BeautifulSoup+正则+Python 抓取网页数据
- 《Python数据挖掘入门与实战》第四章电影推荐案例
- 免费馅饼 HDU
- html无序有序列表嵌套
- mysql 乱码之 Incorrect string value: 'XXXXXX' for column 'XXX' at row 1 类问题
- opencv(二):FileStorage类
- python爬数据小试牛刀--beautifulSoup使用
- 关于js对象键顺序的更改
- 通俗理解KMP字符串匹配算法
- 494. Target Sum
- Java并发编程(三)——线程的状态
- Linux CRC校验码
- 用c++简单的封装线程c中互斥锁
- PAT (Advanced Level) Practise 1120 Friend Numbers (20)
- C# ACCESS数据库操作类