rookiedude rookiedude - 1 month ago 15
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.

Thanks

This is what i try to get.

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


value always contains random numbers.

Answer

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 : https://www.crummy.com/software/BeautifulSoup/bs4/doc/

You can install it like this :

[python_binary] -m pip install bs4
Comments