糗事百科实例

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)

糗事百科实例