当前位置: 首页 > news >正文

Python爬虫(十七)_糗事百科案例

糗事百科实例

爬取糗事百科段子,假设页面的URL是: http://www.qiushibaike.com/8hr/page/1

要求:

  1. 使用requests获取页面信息,用XPath/re做数据提取
  2. 获取每个帖子里的用户头像连接、用户姓名、段子内容、点赞次数和评论次数
  3. 保存到json文件内

参考代码

#-*- coding:utf-8 -*-import requests
from lxml import etreepage = 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.texthtml = 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, commentsexcept Exception, e:print e

演示效果

糗事百科

糗事百科

http://www.lryc.cn/news/148425.html

相关文章:

  • Ae 效果:CC Threads
  • Kotlin 协程 - 多路复用 select()
  • 学习笔记-ThreadLocal
  • python利用pandas统计分析—groupby()函数的使用
  • OPENCV实现ORB特征检测
  • W5100S-EVB-PICO主动PING主机IP检测连通性(十)
  • 使用 Nginx 搭建文件下载服务器
  • 链式栈StackT
  • Fiddler中 AutoResponder 使用
  • 77GHz线性调频连续波雷达
  • YOLOV8改进:更换为MPDIOU,实现有效涨点
  • BookStack开源免费知识库docker-compose部署
  • Linux:编译遇到 Please port gnulib freadahead.c to your platform ,怎么破
  • three.js(三):three.js的渲染结构
  • 客户端读写HBase数据库的运行原理
  • 不使用VH6501设备,通过VN1630等普通设备使用canConfigureBusOff函数进行busoff干扰测试
  • 服务器数据恢复-服务器RAID6硬盘故障离线的数据恢复案例
  • DB2 HADR+TSA运维,TSA添加资源组的命令
  • LeetCode-135-分发糖果
  • Viva Workplace Analytics Employee Feedback SU Viva Glint部署方案
  • ASIC-WORLD Verilog(14)系统任务
  • 两台电脑共享文件设置
  • 《C和指针》笔记17:sizeof
  • 说说大表关联小表
  • Unity 之 方括号[ ] 的用法以及作用
  • 微服务nacos或者yml配置内容部分加密jasypt
  • Vue:插槽,与自定义事件
  • Window11-Ubuntu双系统安装
  • 【React】React学习:从初级到高级(一)
  • Flutter 安装教程 + 运行教程