[Python] Get elements by specifying attributes with prefix search in BeautifulSoup

I was a little addicted to getting the element whose attribute value is dynamic when scraping, so share it.

HTML sample

.html


<div class="hogehoge">
    <input type="hidden" id="sample_1" value="fugafuga">
    <input type="hidden" id="sample_2" value="fugafuga">
    <input type="hidden" id="sample_3" value="fugafuga">
    <input type="hidden" id="sample_4" value="fugafuga">
    <input type="hidden" id="sample_5" value="fugafuga">
・
・
・
</div>

Suppose you want to get an element whose number of elements is indefinite and whose attribute value is dynamic in this way.

Acquisition method

** ・ lambda function ver **

source = soup.find_all('input', id=lambda value: value and value.startswith('sample_'))

You can get the element by prefix search using the lambda function.

** ・ Regular expression ver **

source = soup.find_all('input', id=re.compile('^sample_'))

It can also be obtained with a regular expression.

** ・ CSS selector ver **

source = soup.select('input[id^=sample_]')

It can also be specified with the CSS selector.

Reference link

How to find all divs who's class starts with a string in BeautifulSoup?

Recommended Posts

[Python] Get elements by specifying attributes with prefix search in BeautifulSoup
[Python] Get element by specifying name attribute in BeautifulSoup
Get started with Python in Blender
Get additional data in LDAP with python
Get property information by scraping with python
Get Started with TopCoder in Python (2020 Edition)
[Python] Get the files in a folder with Python
Sequential search with Python
Binary search in Python
[Python] Get the numbers in the graph image with OCR
Get standard output in real time with Python subprocess
Revive symbol search in Python workspace with VS Code
Get the result in dict format with Python psycopg2
Get the size (number of elements) of UnionFind in Python
Linear search in Python
Binary search with python
Binary search with Python3
Get date in Python
Open page in secret mode by specifying browser language with Selenium Python Bindings (Chrome)
Get date with python
JPEG image generation by specifying quality with Python + OpenCV
Group by consecutive elements of a list in Python
[Python] Delete by specifying a tag with Beautiful Soup
Get in touch with functional programming in JavaScript or Python 3
I made a class to get the analysis result by MeCab in ndarray with python
Get the number of specific elements in a python list
Get the value while specifying the default value from dict in Python
Get message from first offset with kafka consumer in python
Get YouTube Comments in Python
Get country code with python
Scraping with selenium in Python
Get last month in python
Working with LibreOffice in Python
Get Twitter timeline with python
Debugging with pdb in Python
Get Youtube data with python
Search for strings in Python
Working with sounds in Python
Perform prefix search in DynamoDB
Binary search in Python / C ++
Scraping with Tor in Python
Tweet with image in Python
Full bit search with Python
Get Terminal size in Python
Explicitly get EOF in python
Combined with permutations in Python
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Search engine work with python
Search twitter tweets with python
Get stock price with Python
Get Evernote notes in Python
Getting list elements in Python
Get home directory with python
Get keyboard events with python
Streamline web search with python
Get Alembic information with Python
Sort by date in python
Get Japanese synonyms in Python
Get the link destination URL by specifying a text sentence with Python scraping (Beautiful Soup) + XPath
How is the progress? Let's get on with the boom ?? in Python