Answer the question
In order to leave comments, you need to log in
How to parse product prices on a DNS site?
Hello! Please help me, I'm a novice Python programmer, like all self-taught people I encounter a lot of problems, here is one of them --> I decided to parse the laptop directory from the DNS site and all the data can be parsed except for the price, for some reason it gives out -->
'product-price': item.find('div', class_='product-price').get_text()
AttributeError: 'NoneType' object has no attribute 'get_text'
import requests
from bs4 import BeautifulSoup
import csv
from fake_useragent import UserAgent
ua = UserAgent()
CSV = 'Ноутбуки DNS.csv'
URL = 'https://www.dns-shop.ru/catalog/17a892f816404e77/noutbuki/'
HEADERS = {'user-agent': ua.random}
def get_html(url, params=''):
r = requests.get(url, headers=HEADERS, params=params)
return r
# catalog-item
def get_content(html):
soup = BeautifulSoup(html.text, 'html.parser')
items = soup.find_all('div', class_='n-catalog-product')
notebook = []
for item in items:
notebook.append({
'title': item.find('a', class_='ui-link').get_text(),
'product-info': item.find('span', class_='product-info__title-description').get_text(),
'product-price': item.find('div', class_='product-price').get_text()
})
return notebook
def parser():
html = get_html(URL)
print(get_content(html))
if __name__ == '__main__':
parser()
Answer the question
In order to leave comments, you need to log in
BeautifulSoup(html.text, 'html.parser')
try BeautifulSoup(html.text, 'lxml') (install lxml library first)
can
item.find('a', class_='ui-link'). text
for the future - read "clean code" and at least just any python book.
it is not advisable to use variables as you have declared them.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question