Python爬虫(十七)_糗事百科案例
2017-12-21 18:26
323 查看
糗事百科实例
爬取糗事百科段子,假设页面的URL是: http://www.qiushibaike.com/8hr/page/1要求:
使用requests获取页面信息,用XPath/re做数据提取获取每个帖子里的用户头像连接、用户姓名、段子内容、点赞次数和评论次数
保存到json文件内
参考代码
#-*- coding:utf-8 -*- import requests from lxml import etree page = 1 url = 'http://www.qiushibaike.com/8hr/page/' + str(page) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', 'Accept-Language': 'zh-CN,zh;q=0.8'} try: response = requests.get(url, headers=headers) resHtml = response.text html = etree.HTML(resHtml) result = html.xpath('//div[contains(@id,"qiushi_tag")]') for site in result: item = {} imgUrl = site.xpath('./div//img/@src')[0].encode('utf-8') # print(imgUrl) username = site.xpath('./div//h2')[0].text # print(username) content = site.xpath('.//div[@class="content"]/span')[0].text.strip().encode('utf-8') # print(content) # 投票次数 vote = site.xpath('.//i')[0].text # print(vote) #print site.xpath('.//*[@class="number"]')[0].text # 评论信息 comments = site.xpath('.//i')[1].text # print(comments) print imgUrl, username, content, vote, comments except Exception, e: print e
演示效果
相关文章推荐
- Python爬虫(十八)_多线程糗事百科案例
- python爬虫小案例(逻辑规范的第一步)---糗事百科
- python爬虫案例——糗事百科数据采集
- [Python]糗事百科的网络爬虫(v0.4)源码
- [Python]网络爬虫(八):糗事百科的网络爬虫(v0.2)源码及解析
- python人工智能和大数据爬虫案例分享
- Python爬虫(十三)_案例:使用XPath的爬虫
- python爬虫糗事百科
- Python爬虫(二十四)_selenium案例:执行javascript脚本
- Python爬虫实现爬取糗事百科
- Python爬虫——8-1.scrapy深度爬取案例—百思不得姐
- python爬虫数据-下载图片经典案例
- python3 爬虫之爬取糗事百科
- Python-简单的爬虫案例(百度贴吧-图片)
- Python爬虫(入门+进阶)学习笔记 1-8 使用自动化神器Selenium爬取动态网页(案例三:爬取淘宝商品)
- Python爬虫:获取糗事百科笑话
- python爬虫:案例四:新浪微指数(未完全解决
- Python爬虫实战案例:爬取爱奇艺VIP视频
- python链接爬虫案例
- [python爬虫] 抓取糗事百科的爬虫程序