Python Mechanize:如何输入TextControl并单击ImageControl图标?

时间:2014-05-23 03:32:18

标签: python mechanize mechanize-python

我正在尝试将邮政编码信息输入http://www.gasbuddy.com/并使用Mechanize

点击“搜索”

所以我能够用以下数据抓取表格:

<TextControl(ctl00$Content$GBZS$txtZip=City, Province or Postal Code...)>
<ImageControl(ctl00$Content$GBZS$btnSearch=)>

我能够将文本插入TextControl(我假设),这将表单更改为:

<TextControl(ctl00$Content$GBZS$txtZip=ABC 123)>
<ImageControl(ctl00$Content$GBZS$btnSearch=)>

使用以下代码:

browser.select_form(nr=0)
browser["ctl00$Content$GBZS$txtZip"] = "ABC 123"

for form in browser.forms():
    print list(browser.forms())[0];

所以我的问题是如何点击ImageControl?我试过做一个browser.submit(),但它不起作用。

1 个答案:

答案 0 :(得分:1)

mechanize无法提交表单:

  • 根本没有submit按钮
  • 单击图像控件时涉及到javascript:

    <input type="image" name="ctl00$Content$GBZS$btnSearch" id="ctl00_Content_GBZS_btnSearch" class="zs_img" src="/images/art/search-84x23-bt.png" alt="Gas Price Search" 
     onclick="javascript:if(btnSearch_click(event) == false){return false;};WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$Content$GBZS$btnSearch&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Search.aspx&quot;, false, false))" style="border-width:0px;">
    

以下是您可以做的事情:

  • 只需获取搜索后打开的页面 - 只需将城市名称粘贴到网址中即可。例如,对于New York City,网址为:http://www.newyorkgasprices.com/New%20York%20City/index.aspx

    import mechanize
    from urllib2 import quote
    
    url = "http://www.newyorkgasprices.com/%s/index.aspx"
    city = "New York City"
    url = url % quote(url)
    
    browser = mechanize.Browser()
    browser.open(url)
    
    browser.title()
    
  • 使用selenium的真实浏览器:

    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.get('http://www.gasbuddy.com/')
    
    city = 'New York City'
    
    textinput = browser.find_element_by_id('ctl00_Content_GBZS_txtZip')
    textinput.send_keys(city)
    
    button = browser.find_element_by_id('ctl00_Content_GBZS_btnSearch')
    button.click()
    
    print browser.title
    
    browser.close()
    

两个片段打印:

'New York City Gas Prices - Find Cheap Gas Prices in New York City, New York'