我想从汽车博客上得到一些评论,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()
使用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的情况下自动化该过程。在
相关问题 更多 >
编程相关推荐