Проблема с просмотром веб-страниц, как отобразить данные с двух разных сайтов в одном файле html - PullRequest
0 голосов
/ 14 марта 2020

У меня проблема с отображением данных с двух разных сайтов в одном и том же файле html (в одной таблице).

Я много пробовал, ищу какие-то решения, но мне ничего не помогает. Вы также можете связать меня с любым учебником по python / bs / web-scraping к моим будущим "проблемам": D

Заранее спасибо.

КОД:

import pandas as pd
import requests
from bs4 import BeautifulSoup

odpowiedz = requests.get(
    "https://www.nike.com/pl/w?q=react%20270&vst=react%20270")
soup = BeautifulSoup(odpowiedz.text, 'html.parser')

items = soup.find_all(
    class_='product-card css-1pclthi ncss-col-sm-6 ncss-col-lg-4 va-sm-t product-grid__card')

title = [item.find(class_='product-card__title').get_text()
         for item in items]
price = [item.find(class_='product-card__price').get_text()
         for item in items]
linki = [item.find(class_='product-card__link-overlay').attrs['href']
         for item in items]

odpowiedz = requests.get(
    "https://www.nike.com/pl/w/air-max-97-buty-77f38zy7ok")
soup = BeautifulSoup(odpowiedz.text, 'html.parser')

items = soup.find_all(
    class_='product-card css-1pclthi ncss-col-sm-6 ncss-col-lg-4 va-sm-t product-grid__card')

title = [item.find(class_='product-card__title').get_text()
         for item in items]
price = [item.find(class_='product-card__price').get_text()
         for item in items]
linki = [item.find(class_='product-card__link-overlay').attrs['href']
         for item in items]

wynik = pd.DataFrame(
    {
        'Model': title,
        'Cena': price,
        'Link': linki,
    })

print(wynik)
wynik.to_html('official.html')

Результат этой программы является id, название продукта, цена и ссылка (на обувь в этом примере) с 1-го сайта (Nike реагировать), и я хочу добавить данные со второго сайта (Nike Air Max 97) и добавить их также в таблицу с моим первым результат (Nike реагирует)

1 Ответ

0 голосов
/ 15 марта 2020

Определенно есть лучший способ сделать это. Но вот быстрое решение: -

import pandas as pd
import requests
from bs4 import BeautifulSoup


title = []
price = []
linki = []

odpowiedz = requests.get(
    "https://www.nike.com/pl/w?q=react%20270&vst=react%20270")
soup = BeautifulSoup(odpowiedz.text, 'html.parser')

items = soup.find_all(
    class_='product-card css-1pclthi ncss-col-sm-6 ncss-col-lg-4 va-sm-t product-grid__card')

title.append([item.find(class_='product-card__title').get_text()
         for item in items])
price.append([item.find(class_='product-card__price').get_text()
         for item in items])
linki.append([item.find(class_='product-card__link-overlay').attrs['href']
         for item in items])

odpowiedz = requests.get(
    "https://www.nike.com/pl/w/air-max-97-buty-77f38zy7ok")
soup = BeautifulSoup(odpowiedz.text, 'html.parser')

items = soup.find_all(
    class_='product-card css-1pclthi ncss-col-sm-6 ncss-col-lg-4 va-sm-t product-grid__card')

title.append([item.find(class_='product-card__title').get_text()
         for item in items])
price.append([item.find(class_='product-card__price').get_text()
         for item in items])
linki.append([item.find(class_='product-card__link-overlay').attrs['href']
         for item in items])

flat_titles = [titles for sublisttitle in title for titles in sublisttitle]
flat_prices = [prices for sublistprice in price for prices in sublistprice]
flat_links = [links for sublistlinks in linki for links in sublistlinks]

wynik = pd.DataFrame(
    {
        'Model': flat_titles,
        'Cena': flat_prices,
        'Link': flat_links,
    })

print(wynik)
wynik.to_html('official.html')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...