Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grabbing the value of a HTML form input field?

There's a web page similar to: www.example.com/form.php

I want to use Python to grab one of the values from the HTML form on the page. For example, if the form had I could get the value "test" returned

I have googled this extensively but most relate to posting form data, or have advice to use Django or cgi-bin. I don't have access to the server directly so I can't do that.

I thought the library REQUESTS could do it but I can't see it in the documentation.

HTML:

<html>
<body>
<form method="" action="formpost.php" name="form1" id="form1">
<input type="text" name"field1" value="this is field1">
<input type="hidden" name="key" value="secret key field">
</form>
</body>

As an example, I'd like something like this in Python:

import special_library

html = special_library.get("http://www.example.com/form.php")
print html.get_field("wanted")

Has anyone got any suggestions to achieve this? Or any libraries I may not have thought of or been aware of?

like image 304
redrabbit29 Avatar asked Dec 06 '25 08:12

redrabbit29


1 Answers

You can use requests library, and lxml

Try this:

import requests
from lxml import html

s = requests.Session()
resp = s.get("http://www.example.com/form.php")
doc = html.fromstring(resp.text)

wanted_value = doc.xpath("//input[@class='wanted_class_name']/@value")
print(wanted_value)

You can check following resources:

  • requests

  • xpath

like image 78
sashaboulouds Avatar answered Dec 07 '25 21:12

sashaboulouds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!