python - 在 Python 中抓取 javascript 呈现的网站的 "Script part"

标签 python selenium

我正在做一个项目,我想在 Reddit 上的各个 subreddits 上查看用户的平均业力。因此,我正在收集用户的业力,这在新的 reddit 结构中被证明有点困难。

我无法使用 PRAW,因为那里的业力数字不正确。

根据一个用户的页面源,我只需要找到以下两个变量:commentKarma和postKarma。这两个变量都位于“”部分下,请参见此处的示例查看源:https://www.reddit.com/user/loganb3171 .但是,当我使用 selenium page_source 或 beautifulsoup 时,它们不会出现。

我已经研究这个问题几个小时了,但我离它还很远。

感谢任何帮助。

这些片段中的任何一个都没有像右键单击“查看页面源代码”时那样给我整个页面源代码

source_var = driver.execute_script("return document.getElementsByTagName('html')[0].innerHTML")

source_var=driver.page_source

最佳答案

好的,所以我看到您使用的是问题片段中的 selenium。如果是这种情况,则无法使用 Web 驱动程序设置请求 header 。 Reddit 会知道你是一个机器人。

如果只需要页面源码,可以使用requests获取页面用selenium打开或者使用BeautifulSoup解析页面

from bs4 import BeautifulSoup
import requests

url = "https://www.reddit.com/user/loganb3171"
page = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(page.text, 'html.parser')

print(soup.prettify())

关于python - 在 Python 中抓取 javascript 呈现的网站的 "Script part",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51121973/

相关文章:

python - Nginx Django 和 Gunicorn。 Gunicorn socks 文件丢失了吗?

python - 在 Python Selenium WebDriver 中按属性查找元素

java - 使用此模板找不到 div xpath

python - 为什么不在我的 GPU 上运行 OpenCL (Ubuntu)

python - Archlinux 上用于 Matplotlib 的 PyQt4、PyQt5 或 PySide

Python数据帧: Standard deviation of last one year of data

python - Pyspark - 将多列数据组合成一个跨行分布的列

javascript - 用 Selenium 刮4

python - 使用 selenium 和 python 将文本发送到 ace 编辑器

text - 如何使用 selenium IDE 验证是否存在任何文本