Issue
I'm having an issue with using the .findAll
attribute. when I run the code below, it says object has no attribute findAll
.
import requests
from bs4 import BeautifulSoup
import urllib3
urllib3.disable_warnings()
url = 'https://csgostash.com/weapon/Butterfly+Knife'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
results = soup.find(id="row")
job_elems = results.findAll('section', class_="well result-box nomargin")
for job_elem in job_elems:
title_elem = jobelem.find('h3', class_='a')
print(title_elem.text.strip())
Error message
job_elems = results.findAll('section', class_="well result-box nomargin") AttributeError: 'NoneType' object has no attribute 'findAll'
Solution
Looking at the HTML for the page you're trying to scrape, it's apparent that no elements have id="row"
; hence, you get the error that you cannot call findAll
on None
.
You could skip that step and go straight to finding 'div'
(instead of 'section'
) elements with class_='well result-box nomargin'
:
job_elems = soup.find_all('div', class_="well result-box nomargin")
for job_elem in job_elems:
title_elem = job_elem.find('a')
if title_elem:
print(title_elem.text.strip())
Output:
★ (Vanilla)
Marble Fade
Doppler
Tiger Tooth
Damascus Steel
Ultraviolet
Rust Coat
Fade
Slaughter
Crimson Web
Night
Case Hardened
Blue Steel
Boreal Forest
Stained
Forest DDPAT
Urban Masked
Scorched
Safari Mesh
Note that there are several mistakes in your code, including jobelem
instead of job_elem
. You also attempt to get the text of the h3
element, though you are actually looking for the a
element nested inside that.
Answered By - dspencer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.