繁体   English   中英

无法在 Python 中获取带有 beautifulSoup 的商店列表

[英]Can't get a list of shops with beautifulSoup in Python

我有一个代码,它应该给我一个商店数据列表。 但是列表是空的并且没有显示任何错误......有什么想法吗?

import requests
from bs4 import BeautifulSoup 
import pandas as pd

def get_page_data(number):
    print('number:', number)

    url = 'https://www.brw.pl/siec-sprzedazy/?page={}'.format(number)
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')

    container = soup.find(class_='lista-salonow')
    items = container.find_all(class_='salon-kontener')


    dane = []

    for item in items:
        adres = item.find(class_='salon-szczegoly-adres').get_text(strip=True)
        dane.append([adres])

    return dane


wszystkie_dane = []
for number in range(1, 3):
    dane_na_stronie = get_page_data(number)

    wszystkie_dane.extend(dane_na_stronie)

dane = pd.DataFrame(wszystkie_dane, columns=['adres'])
dane.to_csv('brw.csv', index=False)

尝试使用以下方法,使用干净、可靠且需要较少代码的请求来直接从提供的网站获取所需的结果。

  1. 首先,在检查了 Chrome 的网络部分后,我从网站上获取了 API URL(ajax 调用)。
  2. 执行 GET 请求以从 API 调用中获取数据。
  3. 将其转换为 JSON。
  4. 最后循环内容。

您可以在浏览器中点击 URL 以查看所有列的结果,然后您可以根据您的要求使用它们。 现在我在 print 语句中只获取 5 个,就像你可以获取其他列一样。

import json
import requests
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

def scrap_shops_data():
    api_url = 'https://www.brw.pl/ajax/zpLIv5maeKSYy8KP07immqanj-PVnJO6mQ/' #API URL to fetch data in JSON form

    shops_result = requests.get(api_url,verify=False).json() #Get request to fetch the data from the supplied URL
    for shop in shops_result: #loop to iterate on the JSON object
        print('-' * 100)
        print(shop['nazwa_salonu'])
        print(shop['adres'])
        print(shop['kod_pocztowy'])
        print(shop['miejscowosc'])
        print(shop['email'])
        print('-' * 100)

scrap_shops_data()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM