python爬虫
来源:互联网 发布:怎么购买备案域名 编辑:程序博客网 时间:2024/06/05 19:33
最近在学习爬虫,爬取几个新闻网站,和大家分享一下学习心得,下方展示为爬取湖北应急办网站(http://yj.hubei.gov.cn/)根据需要爬取突发事件的内容。首先我们要先分析网站的特点,明确自己爬取网站的信息,那些是你的目标,我爬取了新闻标题,日期,来源和新闻内容这几项。由于不同的浏览器版本不同,可能会存在微小的差别,我最开始使用的是360浏览器,然而发现并不能审查元素,由于本人是一个小菜鸟,后来得知了F12打开开发者模式,获取一些内容,比如常用的
select方法和find_all方法。select方法是通过CSS选择器
soup.select("head > title")# [<title>The Dormouse's story</title>]soup.select("p > a")# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]soup.select("p > a:nth-of-type(2)")# [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]soup.select("p > #link1")# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]soup.select("body > a")# []
id = #
soup.select("#link1")# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>]
class = .
soup.select(".sister")# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
当你找的属性不同时,如上,class和id,使用不同的符号,区分,在使用前台css进行页面渲染的时候一样。 否存在某个属性来查找:
soup.select('a[href]')# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,# <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,# <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
阅读全文
0 0
- python爬虫-->爬虫基础
- [爬虫] Python爬虫技巧
- Python爬虫
- python 爬虫
- python 爬虫
- python 爬虫
- python爬虫
- Python爬虫
- Python爬虫
- python 爬虫
- Python爬虫
- python爬虫
- python 爬虫
- python 爬虫
- python爬虫
- python爬虫
- python爬虫
- python 爬虫
- [洛谷P1273]有线电视网
- C语言之二叉树(包括遍历的实现)
- ViewPager数据加载错乱的问题
- android自定义view的长按事件的执行时间
- Android AES加密工具类
- python爬虫
- 利用Dozer实现vo-Entiy-Model中属性的复用
- Oracle:查找表的主键,外键,唯一性约束,索引
- Unity--资源管理
- 2017 Multi-University Training Contest
- CSU
- vue.js router的配置
- 老司机找bug的十年心路历程
- 包含类的c++程序