Не знаю как реализовать нужную задачу (парсинг сайтов )

artempyt9n

Новичок
Пользователь
Май 8, 2020
3
0
1
Я спарсил каталог товаров с OLX там название\дата и тд.
Мне нужно достать и этого товара описание но оно находится уже на другой странице по ссылке,
как достать описание товара. Вот мой код:

Python:
url = input("ссылка:")
HEADERS = {
    'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Mobile Safari/537.36',
    'accept': '*/*'
}
HOST = 'https://www.olx.ua/'

response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
products = soup.find_all('div', class_='offer-wrapper')

all_products = []


for product in products:
    name = product.find('strong').text
    pricer = product.find('p', class_='price').text.strip()
    link = product.find('a', class_='marginright5 link linkWithHash detailsLink').get('href')
   
    all_products.append([name, pricer, link])

names = ["Название", "Цена", "Ссылка"]
with open("data.csv", "w", newline='') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow(names)
    for product in all_products:
        writer.writerow(product)
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
как достать описание товара
Перейти на страницу товара по ссылке link = product.find('a', class_='marginright5 link linkWithHash detailsLink').get('href') и спарсить оттуда описание.
 

artempyt9n

Новичок
Пользователь
Май 8, 2020
3
0
1
Перейти на страницу товара по ссылке link = product.find('a', class_='marginright5 link linkWithHash detailsLink').get('href') и спарсить оттуда описание.
Вот что такое я намудрил, но оно не собирает данные с другой страницы хз почему:
Python:
import requests
from bs4 import BeautifulSoup as BS
import csv


url = input("ссылка:")
HEADERS = {
    'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Mobile Safari/537.36',
    'accept': '*/*'
}
HOST = 'https://www.olx.ua/'
response = requests.get(url)
html = response.text
soup = BS(html, 'html.parser')
products = soup.find_all('div', class_='offer-wrapper')
link2 = soup.find('a', class_='marginright5 link linkWithHash detailsLink').get('href')
response2 = requests.get(link2)
html2 = response2.text
soup2 = BS(html2, 'html.parser')
opisanies = soup2.find_all('div', class_='offerdescription clr')

all_products = []
all_opisai = []

for opisanie in opisanies:
    op = opisanie.find('div', class_='descriptioncontent__headline').text
    all_opisai.append([op])
    

for product in products:
    name = product.find('strong').text
    pricer = product.find('p', class_='price').text.strip()
    link = product.find('a', class_='marginright5 link linkWithHash detailsLink').get('href')
    
    
    all_products.append([name, pricer, link])

names = ["Название", "Цена", "Ссылка", "Описание"]
with open("data.csv", "w", newline='') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow(names)
    for product in all_products:
        writer.writerow(product)
    for opisanie in all_opisai:
        writer.writerow(opisanie)
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Вот что такое я намудрил, но оно не собирает данные с другой страницы хз почему:
Вот пример (с другой страницы собирает заголовок):
Python:
import requests
from bs4 import BeautifulSoup as BS
import csv

url = 'https://www.olx.ua/elektronika/kompyutery-i-komplektuyuschie/'
HEADERS = {
    'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Mobile Safari/537.36',
    'accept': '*/*'
}
HOST = 'https://www.olx.ua/'

response = requests.get(url)
html = response.text
soup = BS(html, 'html.parser')
products = soup.find_all('div', class_='offer-wrapper')

all_products = []


for product in products:
    name = product.find('strong').text
    pricer = product.find('p', class_='price').text.strip()
    link = product.find('a', class_='marginright5 link linkWithHash detailsLink').get('href')
  
    all_products.append([name, pricer, link])

names = ["Название", "Цена", "Ссылка", "Описание"]

with open("oxl.csv", "w", encoding='utf-8', newline='') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow(names)
    for product in all_products:
        response = requests.get(product[-1])
        html = response.text
        soup = BS(html, 'html.parser')
        #opisanie = soup.find('div', id='textContent').text
        opisanie = soup.find('h1').text

        writer.writerow(product + [opisanie.strip()])
 

artempyt9n

Новичок
Пользователь
Май 8, 2020
3
0
1
спасибо, что помогли!!!
 

Форум IT Специалистов