糗事百科实例

import requests
from lxml import etree

page=1 #爬取第一页
base_url=‘https://www.qiushibaike.com/8hr/page/%d/’%page #请求地址
headers={‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36’} #user-agent

try:
response=requests.get(base_url,headers=headers)
resHtml=response.text #响应的内容

html=etree.HTML(resHtml)  #转化为树形结构
li_list=html.xpath('//li[@class="item typs_video"]')
for li in li_list:
    name=li.xpath('./div/div/a/img/@alt')  #用户名
    content=li.xpath('./div/a/text()')  #内容
    zan=li.xpath('./div/div/div/span[position()=1]/text()') #点赞
    pinlun=li.xpath('./div/div/div/span[last()-1]/text()') #评论次数
    lianjie=li.xpath('./a/img/@src')  #链接
    print('用户名:',name[0],'点赞:',zan[0],'评论:',pinlun[0])
    print('内容:',content[0])
    print(lianjie[0])
    print('============================================================')

except Exception as e:
print(e)

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值