模拟点击python中的javascript链接 [英] Simulating clicking on a javascript link in python
问题描述
我正在尝试整理餐馆的评论。 Urllib2适用于评论的初始页面,但是有一个链接可以加载下一个评论增量,这是一个javascript链接。示例页面是此处,以及链接的代码下一个25是:
I am trying to collate reviews of restaurants. Urllib2 works fine for the initial page of reviews, but there is then a link to load the next increment of comments which is a javascript link. An example page is here, and the code for the link "Next 25" is:
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$RestRatings$Next','')" class="red" id="ctl00_ContentPlaceHolder1_RestRatings_Next">NEXT 25>> </a>
我查看了之前的所有答案(例如),我不得不说我不是更聪明。在Firebug中查看控制台并不能提供方便的链接。你能建议最好(最简单)的方法吗?
I have looked at all the previous answers (e.g.), and I have to say I'm none the wiser. Looking at the console in Firebug doesn't offer up a handy link. Could you suggest the best (easiest) way to achieve this?
编辑:
感谢Seleniumnewbie,这段代码将打印出评论中的所有评论。 :
With thanks to Seleniumnewbie this code will print out all the comments from the reviews.:
from selenium import webdriver
from BeautifulSoup import BeautifulSoup
import re
driver = webdriver.Firefox()
def getURLinfo(url):
driver.get(url)
html = driver.page_source
next25 = "ctl00_ContentPlaceHolder1_RestRatings_Next"
soup = BeautifulSoup(html)
while soup.find(id=re.compile(next25)):
driver.find_element_by_id(next25).click()
html = html + driver.page_source
soup = BeautifulSoup(driver.page_source)
soup = BeautifulSoup(html)
comment = soup.findAll(id=re.compile("divComment"))
for entry in comment:
print entry.div.contents #for comments
driver.close()
推荐答案
按 id =ctl00_ContentPlaceHolder1_RestRatings_Next
查找元素,然后单击它。
Find the element by id="ctl00_ContentPlaceHolder1_RestRatings_Next"
and then click it.
这篇关于模拟点击python中的javascript链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!