[Python] Get element by specifying name attribute in BeautifulSoup

A story when you want to specify the name attribute when getting the html element with BeautifulSoup.

Basic find

#Acquisition example
source = soup.find('div', class_='hogehoge')

Basically, get by specifying soup.find ('tag name', attribute name ='value') in this way. However, if you write this in the name attribute, an error will occur.

#Acquisition example
source = soup.find('input', name='hogehoge', type='hidden')

Execution result


TypeError: find() got multiple values for keyword argument 'name'

This seems to be an error because the argument name is already defined in the find method of BeautifulSoup.

Description when specifying the name attribute

source = soup.find('input', attrs={'name': 'hogehoge', 'type': 'hidden'})

It seems that you can specify the name attribute by passing a dictionary type value to the argument attrs, so specify it with this.

source = soup.find('input', {'name': 'hogehoge', 'type': 'hidden'})

You can get the same result even if you omit the argument, so choose the one you like.

Reference link

Parameters for find function [Python: BeautifulSoup-Get attribute values based on name attributes](https://www.it-swarm.dev/ja/python/python%EF%BC%9Abeautifulsoup%E5%90%8D%E5% 89% 8D% E5% B1% 9E% E6% 80% A7% E3% 81% AB% E5% 9F% BA% E3% 81% A5% E3% 81% 84% E3% 81% A6% E5% B1% 9E% E6% 80% A7% E5% 80% A4% E3% 82% 92% E5% 8F% 96% E5% BE% 97% E3% 81% 97% E3% 81% BE% E3% 81% 99 / 1068412706 /)

Recommended Posts

[Python] Get element by specifying name attribute in BeautifulSoup
[Python] Get elements by specifying attributes with prefix search in BeautifulSoup
Get the host name in Python
Get date in Python
Get the last element of the array by splitting the string in Python and PHP
How to get the variable name itself in python
Get YouTube Comments in Python
Get last month in python
Get Terminal size in Python
Explicitly get EOF in python
Get Evernote notes in Python
Sort by date in python
Get Japanese synonyms in Python
Get the value while specifying the default value from dict in Python
Get the index of each element of the confusion matrix in Python
Get Leap Motion data in Python.
Get data from Quandl in Python
Get the desktop path in Python
Get the script path in Python
Make Python dict accessible by Attribute
Get, post communication memo in Python
I get a can't set attribute when using @property in python
Get the desktop path in Python
Rectangle area element split in Python
Sort by specifying conditions in CASTable
Get started with Python in Blender
How to sort by specifying a column in the Python Numpy array.
When specifying multiple keys in python sort
Get additional data in LDAP with python
Python> dictionary> values ()> Get All Values by Using values ()
Get html from element with Python selenium
Get Suica balance in Python (using libpafe)
[Python] Get the variable name with str
Get Google Fit API data in Python
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
Read the file line by line in Python
Automate jobs by manipulating files in Python
Read the file line by line in Python
I can't get the element in Selenium!
Get battery level from SwitchBot in Python
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Common mock by moto in Python Unittest
Get the EDINET code list in Python
Get Precipitation Probability from XML in Python
Get Cloud Logging available in Python in 10 minutes
Manipulate namespaced XML in Python (Element Tree)
Alignment algorithm by insertion method in Python
Get rid of DICOM images in Python
Scene recognition by GIST features in Python
Get metric history from MLflow in Python
Get your own IP address in Python
How to get the "name" of a field whose value is limited by the choice attribute in Django's model
Get Unix time of the time specified by JST regardless of the time zone of the server in Python
I want to get the file name, line number, and function name in Python 3.4
How to use loc / iloc / ix to get by specifying a column in CASTable
Get time series data from k-db.com in Python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
Get the caller of a function in Python