Answer the question
In order to leave comments, you need to log in
When parsing, it throws an error, although everything is parsed and cannot parse links to profiles?
from bs4 import BeautifulSoup
import requests
def parse():
URL = 'https://www.olx.pt/tecnologia-e-informatica/'
HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
}
response = requests.get(URL,
headers=HEADERS)
soup = BeautifulSoup(response.content, 'html.parser')
items = soup.findAll('div', class_='offer-wrapper')
comps = []
for item in items:
olx_ship = item.find('span', class_='promo-label promo-label--ctt inlblk rel')
if olx_ship:
pass
else:
pass
try:
comps.append(
{
#получение названия
'title': item.find('a', class_='marginright5 link linkWithHash detailsLink').get_text(strip=True),
#получения цены
'price': item.find('p', class_='price').get_text(strip=True),
# получения ссылки
'link': item.find('a', class_='marginright5 link linkWithHash detailsLink').get('href'),
# получения города
'city': item.find('small', class_='breadcrumb x-normal').find_next('span').get_text(strip=True)
})
except:
pass
for user in comps:
try:
r = requests.get(user["link"], headers=HEADERS)
soup_2 = BeautifulSoup(r.text, 'html.parser')
name = soup_2.find('a', class_='user-offers').get('href')
comps.append({'user': name})
except:
pass
for comp in comps:
print(f'{comp["title"]} -> Price: {comp["price"]} -> Link: {comp["link"]} -> City: {comp["city"]}')
# -> Price: {comp["price"]} -> Link: {comp["link"]} -> City: {comp["city"]}
parse()
Answer the question
In order to leave comments, you need to log in
You yourself added such a dictionary there. By the way, for such an addition inside the iteration, as well as for such an except, they kick, hard.
And you also learn how to debug, for example, just print comp before the place where the error is. comps.append({'user': name})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question