爬取糗事百科的段子:

1.用xpath分析首要爬去内容的表达式;

2.用发起请求,获得原码;

3.用xpath分析源码,提取有用信息;

4.由python格式转为json格式,写入文件

#_*_ coding: utf-8 _*_
'''
Created on 2018年7月17日
@author: sss
function: 爬取糗事百科里面的内容 ''' import requests
import json
from lxml import etree url = "https://www.qiushibaike.com/8hr/page/3/"
headers = {'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0;'}
html= requests.get(url, headers = headers).text
# print(html) #将返回的字符串格式,转为HTML DOM模式
text = etree.HTML(html) #获得包含每个糗事的链表
#返回所有糗事的节点位置,contains()模糊查询方法,第一个参数为要匹配的标签,第二个参数为标签的内容
node_list = text.xpath('//div[contains(@id, "qiushi_tag_")]') items = {}
for node in node_list:
#用户名
# username = node.xpath('./div/a/h2')[0].text
username = node.xpath('.//h2')[0].text
#图片连接
image = node.xpath('.//img/@src')#[0]
#取出标题下的内容
content = node.xpath('./a/div/span')[0].text
#点赞
zan = node.xpath('./div/span/i')[0].text
#评论
comment = node.xpath('./div/span/a/i')[0].text items = {
'username' : username,
'image' : image,
'content' : content,
'zan' : zan,
'comments' : comment
} #把python格式的转换为json格式,此时转换成了字符串,就可以写入糗事段子.txt文件中了
we=json.dumps(items, ensure_ascii=False)
print(we)
with open('qiushi.txt', 'a', encoding='utf-8') as f: #注意在这里转为utf-8格式
f.write((we + '\n'))

  

效果:

不以json格式存储:

#_*_ coding: utf-8 _*_
'''
Created on 2018年7月17日
@author: sss
function: 爬取糗事百科里面的内容 ''' import requests
import json
from lxml import etree url = "https://www.qiushibaike.com/8hr/page/3/"
headers = {'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0;'}
html= requests.get(url, headers = headers).text
# print(html) #将返回的字符串格式,转为HTML DOM模式
text = etree.HTML(html) #获得包含每个糗事的链表
#返回所有糗事的节点位置,contains()模糊查询方法,第一个参数为要匹配的标签,第二个参数为标签的内容
node_list = text.xpath('//div[contains(@id, "qiushi_tag_")]') items = {}
for node in node_list:
#用户名
# username = node.xpath('./div/a/h2')[0].text
username = node.xpath('.//h2')[0].text.replace('\n','') #用replace去掉\n
#图片连接
image = node.xpath('.//img/@src')#[0]
#取出标题下的内容
content = node.xpath('./a/div/span')[0].text.replace('\n','')
#点赞
zan = node.xpath('./div/span/i')[0].text
#评论
comment = node.xpath('./div/span/a/i')[0].text items = {
'username' : username,
'image' : image,
'content' : content,
'zan' : zan,
'comments' : comment
} #把python格式的转换为json格式,此时转换成了字符串,就可以写入糗事段子.txt文件中了
# we=json.dumps(items, ensure_ascii=False)
# print(we)
# with open('qiushi.json', 'a', encoding='utf-8') as f: #注意在这里转为utf-8格式
# f.write((we + '\n'))
with open('qiushi_wenben.txt', 'a', encoding = "utf-8") as f:
f.write('用户:' + username + '\n')
f.write('图片链接:https:' + image[0] + '\n')
f.write('内容:' + content + '\n')
f.write('赞:' + zan + '\n')
f.write('评论:' + comment + '\n\n')

 效果:

 

最新文章

  1. [LeetCode] Remove Linked List Elements 移除链表元素
  2. CF-补题1
  3. NOIP 2015 游记
  4. Mac Pro 安装 最新版的 SVN 1.9.4
  5. Linux下配置PHP开发环境
  6. iOS:app直播---采集篇
  7. Discuz! 6.x/7.x 全局变量防御绕过导致命令执行
  8. iOS - OC Struct 结构体
  9. MFC对话框程序EDIT类控件的自动换行,垂直滚动条自动下移
  10. HDU 3622 Bomb Game(2-sat)
  11. 如何获取本机IP
  12. javascript焦点图自动播放
  13. iOS开发之视频播放
  14. Spring Boot 负载均衡之外置session状态保存
  15. Java继承--子类的实例化过程
  16. 论文笔记-Squeeze-and-Excitation Networks
  17. systemd 编写服务管理脚本
  18. hdu 5478 (数论)
  19. 用了一天的时间,linux下expect实现ssh自动登录服务器记,鄙视下网上各种抄来抄去残段子
  20. Python + Appium 获取当前屏幕的截图方法的封装

热门文章

  1. ***XX-net 和 proxyee-down
  2. warning: backslash and newline separated by space [enabled by default]
  3. dateframe行列插入和删除操作
  4. WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一)
  5. STM32之中断
  6. RTMP(实时信息传输协议)详解
  7. android 网络连接判断
  8. python接口自动化21-下载excel文件(Content-Type:octets/stream)
  9. spring AOP 之五:Spring MVC通过AOP切面编程来拦截controller
  10. 04:Sysbench压测-innodb_flush_log_at_trx_commit,sync_binlog参数对性能的影响