# -*- coding = utf-8 -*- # @Time : 2021/8/23 19:01 # @Author : xiao long # @File :weibo.py import requests from lxml import etree url = 'https://s.weibo.com/top/summary?cate=realtimehot' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'} response = requests.get(url,headers=headers).content.decode() html = etree.HTML(response) #第一种方法 sequences = html.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr/td[@class="td-01 ranktop"]/text()')[0:] titles = html.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr[position()>1]/td[2]/a/text()')[0:] heats = html.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr/td[2]/span/text()')[0:] for sequence,title,heat in zip(sequences,titles,heats): print(f'{sequence}: {title}: {heat}') 上面是第一种方法,下面是第二种方法
# 第二种方法 trs = html.xpath('//tr[position()>1]') for tr in trs: sequence = tr.xpath('.//td[@class="td-01 ranktop"]/text()') title = tr.xpath('.//a/text()') heat = tr.xpath('.//span/text()') print(f'{sequence[0]} {title[0]} {heat}')