我必须从这个网站上为美国前州长搜集所有信息。但是,要读出结果然后点击链接,我需要访问不同的结果页面,或者最好将每页显示的结果限制设置为最大 100(我认为不会超过 100每个州的结果)。但是,页面信息似乎使用 javascript,不是表单的一部分,似乎我无法将其作为控件访问。
有关如何进行的任何信息?我对python很陌生,只偶尔将它用于这样的任务。这是一些遍历主窗体的简单代码。
import mechanize
import lxml.html
import csv
site = "http://www.nga.org/cms/FormerGovBios"
output = csv.writer(open(r'output.csv','wb'))
br = mechanize.Browser()
response = br.open(site)
br.select_form(name="governorsSearchForm")
states = br.find_control(id="states-field", type="select").items
for pos, item in enumerate(states[1:2]):
statename = str([label.text for label in item.get_labels()])
print pos, item.name, statename, len(states)
br.select_form(name="governorsSearchForm")
br["state"] = [item.name]
response = br.submit(name="submit", type="submit")
# now set page limit to 100, get links and descriptions\
# and follow each link to get information
for form in br.forms():
print "Form name:", form.name
print form, "\n"
for link in br.links():
print link.text, link.url