我正在尝试整理对餐馆的评论。Urllib2 适用于评论的初始页面,但随后有一个链接可以加载下一个评论增量,这是一个 javascript 链接。示例页面在这里,链接“Next 25”的代码是:
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$RestRatings$Next','')" class="red" id="ctl00_ContentPlaceHolder1_RestRatings_Next">NEXT 25>> </a>
我已经查看了所有以前的答案(例如),我不得不说我一点也不聪明。在 Firebug 中查看控制台并没有提供方便的链接。您能否建议实现这一目标的最佳(最简单)方法?
编辑:感谢 Seleniumnewbie,此代码将打印出评论中的所有评论。:
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()