gpt4 book ai didi

Python Mechanize : How to input into TextControl and click on ImageControl icon?

转载 作者:太空宇宙 更新时间:2023-11-03 18:22:40 25 4
gpt4 key购买 nike

我正在尝试将邮政编码信息输入到 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(),但它不起作用。

最佳答案

mechanize 无法提交表单:

  • 根本没有提交按钮
  • 点击图像控件涉及 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;">

您可以执行以下操作:

  • 只需获取搜索后打开的页面 - 只需将城市名称粘贴到网址中即可。例如,对于纽约市,URL 为: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'

关于Python Mechanize : How to input into TextControl and click on ImageControl icon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23820547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com