# -*- 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}:
python爬虫之xpath实战:爬取微博热搜
最新推荐文章于 2024-04-24 13:35:29 发布