在带有Selenium的Firefox上点击Javascript链接

2024-05-16 04:31:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从汽车博客上得到一些评论,Jalopnik。它最初不是随网页一起提供的,而是使用一些Javascript检索注释。你只得到特色评论。我需要所有的评论,这样我就可以点击“全部”(在“特色”和“开始新的讨论”之间)并获取它们。在

为了实现自动化,我尝试学习Selenium。我修改了Pypi中的their script,猜测点击链接的代码是link.click()和{}。看起来不像是按了“全部”按钮(显示所有评论)。在

最后,我想下载该版本的HTML进行解析。在

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers/") # Load page
time.sleep(0.2)

link = browser.find_element_by_xpath("//a[@class='tc cn_showall']")
link.click()
browser.save_screenshot('screenie.png')
browser.close()

Tags: fromimportbrowser网页timeselenium评论link
1条回答
网友
1楼 · 发布于 2024-05-16 04:31:54

使用Firefox和Firebug插件,我浏览到http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers。在

然后我打开Firebug控制台并单击ALL;它友好地显示了对http://jalopnik.com/index.php?op=threadlist&post_id=5912009&mode=all&page=0&repliesmode=hide&nouser=true&selected_thread=null的单个AJAX调用

在一个新窗口中打开这个url,我就得到了你正在寻找的评论提要。在

更一般地说,如果您将适当的文章ID替换到该url中,您应该能够在不使用Selenium的情况下自动化该过程。在

相关问题 更多 >