rookiedude rookiedude - 1 year ago 111
Python Question

Best way to get value from html in python?

So i want to scrape attribute value in python and currently i'm using regex but its not that effective so i wanted to know what should i use instead since many says that regex is bad for such thing.


This is what i try to get.

<input type="hidden" name="test" value="99948555">

value always contains random numbers.

Answer Source

I would use BeautifulSoup for this kind of parsing :

from bs4 import BeautifulSoup
html = '<input type="hidden" name="test" value="99948555">'
soup = BeautifulSoup(html, 'html.parser')
print(soup.find('input')['name'], ':', soup.find('input')['value'])
# outputs : "test : 99948555"

See the documentation for usage and examples :

You can install it like this :

[python_binary] -m pip install bs4
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download