Submit data via web form and extract the results

风格不统一 提交于 2019-11-28 17:38:12

No need to use mechanize, just send the correct form data in a POST request.

Also, using regular expression to parse HTML is a bad idea. You would be better off using a HTML parser like lxml.html.

import requests
import lxml.html as lh


def gender_genie(text, genre):
    url = 'http://bookblog.net/gender/analysis.php'
    caption = 'The Gender Genie thinks the author of this passage is:'

    form_data = {
        'text': text,
        'genre': genre,
        'submit': 'submit',
    }

    response = requests.post(url, data=form_data)

    tree = lh.document_fromstring(response.content)

    return tree.xpath("//b[text()=$caption]", caption=caption)[0].tail.strip()


if __name__ == '__main__':
    print gender_genie('I have a beard!', 'blog')

You can use mechanize to submit and retrieve content, and the re module for getting what you want. For example, the script below does it for the text of your own question:

import re
from mechanize import Browser

text = """
My python level is Novice. I have never written a web scraper 
or crawler. I have written a python code to connect to an api and 
extract the data that I want. But for some the extracted data I want to 
get the gender of the author. I found this web site 
http://bookblog.net/gender/genie.php but downside is there isn't an api 
available. I was wondering how to write a python to submit data to the 
form in the page and extract the return data. It would be a great help 
if I could get some guidance on this."""

browser = Browser()
browser.open("http://bookblog.net/gender/genie.php")

browser.select_form(nr=0)
browser['text'] = text
browser['genre'] = ['nonfiction']

response = browser.submit()

content = response.read()

result = re.findall(
    r'<b>The Gender Genie thinks the author of this passage is:</b> (\w*)!', content)

print result[0]

What does it do? It creates a mechanize.Browser and goes to the given URL:

browser = Browser()
browser.open("http://bookblog.net/gender/genie.php")

Then it selects the form (since there is only one form to be filled, it will be the first):

browser.select_form(nr=0)

Also, it sets the entries of the form...

browser['text'] = text
browser['genre'] = ['nonfiction']

... and submit it:

response = browser.submit()

Now, we get the result:

content = response.read()

We know that the result is in the form:

<b>The Gender Genie thinks the author of this passage is:</b> male!

So we create a regex for matching and use re.findall():

result = re.findall(
    r'<b>The Gender Genie thinks the author of this passage is:</b> (\w*)!',
    content)

Now the result is available for your use:

print result[0]

You can use mechanize, see examples for details.

from mechanize import ParseResponse, urlopen, urljoin

uri = "http://bookblog.net"

response = urlopen(urljoin(uri, "/gender/genie.php"))
forms = ParseResponse(response, backwards_compat=False)
form = forms[0]

#print form

form['text'] = 'cheese'
form['genre'] = ['fiction']

print urlopen(form.click()).read()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!