Beautiful Soup find element with multiple classes
I'm guessing the soup in show = soup.find() is
source = requests.get(URL to get).text
soup = BeautifulSoup(source, 'lxml')
try:
show = soup.find('div', class_='action-link showPhonesLink').text
.text doesn't always work, but I've found the result doesn't really change without it.
i could give a more help answer if you could provide a little more details.
Include multiple class names in findAll in BeautifulSoup4
I would make a simple CSS selector:
soup.select('div[class="cb-lv-scrs-col cb-font-12 cb-text-complete"],div[class="cb-scag-mtch-status cb-text-inprogress"]')
but, I doubt you really need or should check all of the classes present on an element, would not that be sufficient:
soup.select('div.cb-text-complete,div.cb-text-inprogress')
Beautifulsoup multiple class selector
Use css selectors
instead:
soup.select('div.A.B')
Python bs4 - find_all multiple tags and classes
If you have BS4 4.7.1
or above you can use css
selector.
soup.select('a.full,td.fp.active')
OR
soup.select('.full,.fp.active')
Refering to two classes inside an Html tag using BeautifulSoup 4
The page is loaded dynamically, therefore if we call soup.prettify()
, you see that all the desired output is under the class tab_item
which includes tags that we don't want, and not under the class tab_item app_impression_tracked
.
A different approach would be to use a CSS Selector to find all links under the NewReleasesRows
ID
(<div id="NewReleasesRows">
).
To use a CSS Selector use the select()
method instead of find_all()
(in your example you have used find_all()
instead of select()
)
import requests
from bs4 import BeautifulSoup
URL = "https://store.steampowered.com/tags/fr/RPG/"
soup = BeautifulSoup(requests.get(URL).content, "html.parser")
for tag in soup.select("#NewReleasesRows > a"):
print(tag["href"])
Output:
https://store.steampowered.com/app/1433420/Hero_by_Chance/?snr=1_241_4_rpg_103
https://store.steampowered.com/app/1235140/Yakuza_Like_a_Dragon/?snr=1_241_4_rpg_103
https://store.steampowered.com/app/1445440/Blacksmith_of_the_Sand_Kingdom/?snr=1_241_4_rpg_103
...And on...
BeautifulSoup findAll tags with mutliple classes
Might be a bit slower, but uses the class_
selector:
divs = soup.find_all("div", class_="result-item standard") + soup.find_all("div", class_="result-item standard basic ad")
for div in divs:
item = div.h2.a.text.split()
item_year = item[0]
item_make = item[1]
The syntax you are using is BS3, the syntax in this answer is BS4.
beautiful soup find_all, encompassing multiple class names
The function is given each class
attribute value (str)
; then whole class attribute value (unless no previous call returned for the element). But None
is passed is passed argument if there no class
attribute.
So you need to check None
.
Or for you case simple in
should be enough:
for propbox in soup.find_all('div'):
tester = propbox.find_all('td', {
"class": lambda class_: class_ in ("class1", "class2")
})
# print(tester)
BTW, there's no contains
method, but __contains__
method (in
, membership test operator will use it):
>>> 'haystack'.contains('needle')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'contains'
>>> 'haystack'.__contains__('needle')
False
>>> 'needle' in 'haystack'
False
>>> 'haystack'.__contains__('hay')
True
>>> 'hay' in 'haystack'
True
Related Topics
Replace Empty Strings With None/Null Values in Dataframe
How to Split an Integer into an Array of Digits
Paramiko Capturing Command Output
Parsing Outlook .Msg Files With Python
Import Error: Dll Load Failed in Jupyter Notebook But Working in .Py File
Python Calculate Distance Closest Xy Points
How to Get Current Cpu and Ram Usage in Python
Use Subprocess to Send a Password
Use Cumcount on Pandas Dataframe With a Conditional Increment
How to Truncate the Time on a Datetime Object
Pandas: Update Column Values from Another Column If Criteria
Selenium - Iterating Through Groups of Elements - Python
How to Get Local Issuer Certificate When Using Requests in Python
Convert Float to Float Time in Python
Create a New Dataframe Based on Rows With a Certain Value
How to Store the Result of an Executed Function and Re-Use Later