Python Mechanize javascript

标签 python mechanize mechanize-python

我正在尝试使用 mechanize 从该网站获取纽约北线铁路的价格:
http://as0.mta.info/mnr/fares/choosestation.cfm

问题是,当您选择第一个选项时,网站会使用 JavaScript 来填充您可能的目的地列表。我已经用 python 编写了等效的代码,但我似乎无法让它全部工作。这是我到目前为止所拥有的:

import mechanize
import cookielib
from bs4 import BeautifulSoup

br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1)     Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

br.open("http://as0.mta.info/mnr/fares/choosestation.cfm")

br.select_form(name="form1")
br.form.set_all_readonly(False)

origin_control = br.form.find_control("orig_stat", type="select")
origin_control_list = origin_control.items
origin_control.value = [origin_control.items[0].name]

destination_control_list = reFillList(0, origin_control_list)

destination_control = br.form.find_control("dest_stat", type="select")
destination_control.items = destination_control_list
destination_control.value = [destination_control.items[0].name]

response = br.submit()
response_text = response.read()
print response_text

我知道我没有为您提供 reFillList() 方法的代码,因为它很长,但假设它正确创建了 mechanize.option 对象的列表。 Python 不会提示我任何事情,但在提交时我得到了此警报的 html:

“在线无法提供两条线路之间旅行的票价信息。请调用 511 联系我们的客户信息中心,并要求与代表联系以获取更多信息。”

我在这里遗漏了什么吗?感谢您的帮助!

最佳答案

如果您知道站 ID,则可以更轻松地自行发布请求:

import mechanize
import urllib

post_url = 'http://as0.mta.info/mnr/fares/get_fares.cfm'

orig = 295 #BEACON FALLS
dest = 292 #ANSONIA

params = urllib.urlencode({'dest_stat':dest, 'orig_stat':orig })
rq = mechanize.Request(post_url, params)

fares_page = mechanize.urlopen(rq)

print fares_page.read()

如果您有代码来查找给定起始 ID 的目标 ID 列表(即 refillList() 的变体),则可以对每个组合运行此请求:

import mechanize
import urllib, urllib2
from bs4 import BeautifulSoup

url = 'http://as0.mta.info/mnr/fares/choosestation.cfm'
post_url = 'http://as0.mta.info/mnr/fares/get_fares.cfm'

def get_fares(orig, dest):
    params = urllib.urlencode({'dest_stat':dest, 'orig_stat':orig })
    rq = mechanize.Request(post_url, params)

    fares_page = mechanize.urlopen(rq)
    print(fares_page.read())

pool = BeautifulSoup(urllib2.urlopen(url).read())

#let's keep our stations organised
stations = {}

# dict by station id
for option in pool.find('select', {'name':'orig_stat'}).findChildren():
    stations[option['value']] = {'name':option.string}

#iterate over all routes
for origin in stations:
    destinations = get_list_of_dests(origin) #use your code for this
    stations[origin]['dests'] = destinations

    for destination in destinations:
        print('Processing from %s to %s' % (origin, destination))
        get_fares(origin, destination)

关于Python Mechanize javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11039693/

相关文章:

Python 变量作为 dict 的键

python - 如何在 python 中使用 mechanize 发送带有 post 请求的原始 JSON 数据

python - 如何使用 mechanize 获取网页上的链接并打开这些链接

python - 尝试使用 Python 下载文件时出错

python - 在经过身份验证的 session 中使用 twill/mechanize 检索 application/json 文档

python - 克隆 mechanize.Browser 产生错误

python - python 中的 Mechanizer - 选择没有名称的表单字段

python - 使用 PIL 在 Django 中调整上传文件的大小

python - 对 < 200 个样本的小数据集进行二元分类

python Pandas : read file skipping commented