填写类型文本的输入并使用python按提交

a12*_*773 12 html python beautifulsoup python-2.7

我有这个HTML:

<input type="text" class="txtSearch">
<input type="submit" value="Search" class="sbtSearch">
Run Code Online (Sandbox Code Playgroud)

我需要的是在文本字段中写入,然后单击使用python提交.输入标记不在Form中.我怎么能这样做?

Tha*_*Guy 15

您不必实际填充字段并"点击"提交.您可以模拟提交并获得所需的结果.

在Firefox中使用BeautifulSoup和urllib以及firebug.使用firebug观察网络流量,并从HTTP POST获取提交为dong的post参数.创建一个dict并对其进行url-encode.将其与您的网址请求一起传递.

例如:

from BeautifulSoup import BeautifulSoup
import urllib

post_params = {
    param1 : val1,
    param2 : val2,
    param3 : val3
        }
post_args = urllib.urlencode(post_params)

url = 'http://www.website.com/'
fp = urllib.urlopen(url, post_args)
soup = BeautifulSoup(fp)
Run Code Online (Sandbox Code Playgroud)

参数将根据您尝试提交的内容而更改.在您的代码中做出适当的调整.

  • 您可以考虑更新帖子以包含Python 3. (5认同)

kre*_*tea 8

Here's a selenium solution if you actually need to populate the fields. You would typically only need this for testing purposes, though.

from selenium import webdriver

webpage = r"https://www.yourwebsite.com/" # edit me
searchterm = "Hurricane Sandy" # edit me

driver = webdriver.Chrome()
driver.get(webpage)

sbox = driver.find_element_by_class_name("txtSearch")
sbox.send_keys(searchterm)

submit = driver.find_element_by_class_name("sbtSearch")
submit.click()
Run Code Online (Sandbox Code Playgroud)