python爬虫入门 实战(一)---爬糗事百科
来源:互联网 发布:matlab2014b mac 编辑:程序博客网 时间:2024/06/04 18:54
http://www.jianshu.com/p/3a5b22d3e673
发送请求获取html源码
# -*- coding:utf-8 -*-import urllibimport urllib2page=1url='http://www.qiushibaike.com/hot/page/'+str(page)user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36'headers={'User-Agent':user_agent}request=urllib2.Request(url,headers=headers)response=urllib2.urlopen(request)str_html= response.read()response.close()print str_html
用xpath解析并找到目标结点
我们先尝试从简单的开始,把当前页面的所有段子的作者名字爬下来输出。首先先审查元素,看看页面的结构。
从上图可以看到,所有的段子以瀑布流的形式放在一个id="content-left"
的div里面,段子有共同的特点class="article block untagged mb15"
那我们先获取所有段子的整个div结点
tree=html.fromstring(str_html)nodes=tree.xpath('//div[@id="content-left"]/div[@class="article block untagged mb15"]')
xpath的语法可以参考w3shcool里的XPath 教程,或者其他资料。
然后我们继续看段子的内容结构,如下图:
我们可以发现每个段子的第一个子div就是包含作者(用户)名字和头像信息的div,有共同的特点class="author clearfix",然后具体的名字信息在该div下的子元素第二个a标签里的h2标签里。
了解了结构我们可以进行提取名字了。
tree=html.fromstring(str_html)nodes=tree.xpath('//div[@id="content-left"]/div[@class="article block untagged mb15"]')for node in nodes: res_author=node.xpath('div[@class="author clearfix"]/a[2]/h2')[0] print res_author.text
修改后
# -*- coding:utf-8 -*-from lxml import htmlimport urllibimport urllib2page=1url='http://www.qiushibaike.com/hot/page/'+str(page)user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36'headers={'User-Agent':user_agent}request=urllib2.Request(url,headers=headers)response=urllib2.urlopen(request)str_html= response.read()response.close()tree=html.fromstring(str_html)res_author=tree.xpath('//div[@class="author clearfix"]/a[2]/h2')for author in res_author: print author.text
爬取所有段子的主要信息(作者,内容,点赞和评论数)
# -*- coding:utf-8 -*-from lxml import htmlimport urllibimport urllib2page=1url='http://www.qiushibaike.com/hot/page/'+str(page)user_agent='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36'headers={'User-Agent':user_agent}request=urllib2.Request(url,headers=headers)response=urllib2.urlopen(request)str_html= response.read()response.close()tree=html.fromstring(str_html)nodes=tree.xpath('//div[@id="content-left"]/div[@class="article block untagged mb15"]')#循环对每个段子结点进行输出for node in nodes: #解析每个段子 res_author=node.xpath('div[@class="author clearfix"]/a[2]/h2')[0].text content=node.xpath('a[@class="contentHerf"]/div[@class="content"]/span')[0].text num_vote=node.xpath('div[@class="stats"]/span[@class="stats-vote"]/i')[0].text num_comments=node.xpath('div[@class="stats"]/span[@class="stats-comments"]/a[@class="qiushi_comments"]/i')[0].text #输出指定内容 print u"作者:"+res_author print "---------------------------------------------------------" print u"内容:"+content print "---------------------------------------------------------" print u"点赞:"+num_vote+u",评论:"+num_comments+"\n"
阅读全文
0 0
- python爬虫入门 实战(一)---爬糗事百科
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战之爬糗事百科
- Python爬虫实战(1):爬取糗事百科段子
- Python爬虫实战(1):爬取糗事百科段子
- Python爬虫实战:糗事百科
- Python爬虫(一)——爬取糗事百科
- python爬虫(一)爬取糗事百科
- Python爬虫教程——实战一之爬取糗事百科段子
- Python爬虫教程——实战一之爬取糗事百科段子
- Python爬虫实战一:爬取糗事百科的文本段子
- 静觅 » Python爬虫实战一之爬取糗事百科段子
- Python爬虫实战之爬取糗事百科段子
- Android学习之自带线程池详解
- UVa 679 例题6-6 小球下落(Dropping Balls)
- Docker启动Gitlab
- js常用编辑方法
- 机器学习数据集
- python爬虫入门 实战(一)---爬糗事百科
- QT 信号与槽
- leetcode(53). Maximum Subarray
- 一个将Access数据库转成Mssql数据库的简单工具
- java与C++实现判断闰年(百练OJ:2733:判断闰年)
- Statement与PreparedStatement的区别
- POJ-3273(二分答案)
- 1024. 科学计数法
- intellij idea创建maven项目入门