使用requests.get和lxml从网站获取信息

时间:2020-10-18 18:37:27

标签: python request lxml

所以我正在尝试制作一个简单的程序来查看reddit中的投票和内容,但是我的代码返回了:[]而不是我希望它返回的内容。

from lxml import html
import requests
page = requests.get('https://www.reddit.com/r/AskReddit/comments/j8u0mw/which_colour_can_fuck_right_off/')
tree = html.fromstring(page.content)
upvotes = tree.xpath('//span[@class="_1rZYMD_4xY3gRcSS3p8ODO"]/text()')
print(upvotes)

编辑:我的预期结果是该帖子的投票数量,因此对于该帖子,其应为:"51.7k"

1 个答案:

答案 0 :(得分:0)

尝试将xpath表达式更改为

//div[@data-test-id="post-content"]//button/following-sibling::div/text()