ну — в качестве примера очень распространенного задания Wikipedia-bs4 — мы можем взять вот это:
на этой странице у нас более 600 результаты - на подстраницах:
url = "https://de.wikipedia.org/wiki/Liste_der ... eutschland"
поэтому, чтобы сделать первый экспериментальный сценарий, я следую вот так : сначала я скопирую таблицу со страницы Википедии, а затем преобразую ее в DataFrame Pandas.
поэтому я сначала устанавливаю необходимые пакеты: убедитесь, что у вас установлены запросы, beautifulsoup4 и pandas. Вы можете установить их с помощью pip, если еще этого не сделали:
Код: Выделить всё
pip install requests beautifulsoup4 pandas
Код: Выделить всё
import requests
from bs4 import BeautifulSoup
import pandas as pd
# URL of the Wikipedia page
url = "https://de.wikipedia.org/wiki/Liste_der_Genossenschaftsbanken_in_Deutschland"
# Send a GET request to the URL
response = requests.get(url)
# Parse the HTML content of the page with BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find the first table in the page
table = soup.find('table', {'class': 'wikitable'})
# Initialize an empty list to store the data
data = []
# Iterate over the rows of the table
for row in table.find_all('tr'):
# Get the columns in each row
cols = row.find_all('td')
# If there are columns in the row, get the text from each column and store it in the data list
if cols:
data.append([col.get_text(strip=True) for col in cols])
# Convert the data list to a Pandas DataFrame
df = pd.DataFrame(data, columns=["Bank Name", "Location", "Website"])
# Display the DataFrame
print(df)
# Optionally, save the DataFrame to a CSV file
df.to_csv('genossenschaftsbanken.csv', index=False)
Код: Выделить всё
3 s
# Display the DataFrame
print(df)
# Optionally, save the DataFrame to a CSV file
df.to_csv('genossenschaftsbanken.csv', index=False)
Bank Name Location \
0 BWGV Baden-Württembergischer Genossenschaftsverband...
1 GVB Genossenschaftsverband Bayerne. V.
2 GV Genoverbande. V.
3 GVWE Genossenschaftsverband Weser-Emse. V.
4 FGV Freier Genossenschaftsverband e. V.
5 PDG PDG Genossenschaftlicher Prüfungsverband e. V.
6 Verband der Sparda-Banken e. V.
7 Verband der PSD Banken e. V.
Website
0 Karlsruhe
1 München
2 Frankfurt am Main
3 Oldenburg
4 Düsseldorf
5 Erfurt
6 Frankfurt am Main
7 Bonn
Подробнее здесь: https://stackoverflow.com/questions/787 ... re-in-a-df