Issue
I am learning web scraping and I've run into a problem. I am trying to use the .find() method built into BS4 but it is seeing the code as Pythons built in .find()
I am not sure how to fix it. I have tried using .soup.find() but it didn't seem to fix it either.
Here is my code, I am getting the error on line 9 and 10
from bs4 import BeautifulSoup
import requests
html_text = requests.get('https://www.diyelectronics.co.za/store/353-printers')
soup = BeautifulSoup(html_text.text, 'lxml')
printers = soup.find('ul', class_='product_list grid row')
for printer in printers:
printer_price = printer.find('span', class_='price product-price')
printer_name = printer.find('h5', class_='product-name-container')
print(f'''
Printer Name: {printer_name}
Printer Price: {printer_price}
''')
Solution
If there are multiple <ul>
elements with class of product_list grid row
, you should use .find_all()
instead of .find()
:
printers = soup.find_all('ul', class_='product_list grid row')
Answered By - Behdad Abdollahi Moghadam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.