美麗的湯解析許多條目的列表並保存在資料框中
目前我將從世界各地的教區收集資料。
我的方法適用於 bs4 和 pandas。我目前正在研究抓取邏輯。
import requests from bs4 import BeautifulSoup import pandas as pd url = "http://www.catholic-hierarchy.org/" # Send a GET request to the website response = requests.get(url) #my approach to parse the HTML content of the page soup = BeautifulSoup(response.text, 'html.parser') # Find the relevant elements containing diocese information diocese_elements = soup.find_all("div", class_="diocesan") # Initialize empty lists to store data dioceses = [] addresses = [] # Extract now data from each diocese element for diocese_element in diocese_elements: # Example: Extracting diocese name diocese_name = diocese_element.find("a").text.strip() dioceses.append(diocese_name) # Example: Extracting address address = diocese_element.find("div", class_="address").text.strip() addresses.append(address) # to save the whole data we create a DataFrame using pandas data = {'Diocese': dioceses, 'Address': addresses} df = pd.DataFrame(data) # Display the DataFrame print(df)
目前我的 pycharm 上發現了一些奇怪的東西。 我嘗試找到一種使用pandas 方法收集全部資料的方法。
正確答案
這個範例可以幫助您入門 - 它將解析所有教區頁面以獲取教區名稱 url,並將其儲存到 panda 的 dataframe 中。
然後您可以迭代這些 url 並獲得所需的更多資訊。
import pandas as pd import requests from bs4 import beautifulsoup chars = "abcdefghijklmnopqrstuvwxyz" url = "http://www.catholic-hierarchy.org/diocese/la{char}.html" all_data = [] for char in chars: u = url.format(char=char) while true: print(f"parsing {u}") soup = beautifulsoup(requests.get(u).content, "html.parser") for a in soup.select("li a[href^=d]"): all_data.append( { "name": a.text, "url": "http://www.catholic-hierarchy.org/diocese/" + a["href"], } ) next_page = soup.select_one('a:has(img[alt="[next page]"])') if not next_page: break u = "http://www.catholic-hierarchy.org/diocese/" + next_page["href"] df = pd.dataframe(all_data).drop_duplicates() print(df.head(10))
列印:
... Parsing http://www.catholic-hierarchy.org/diocese/lax.html Parsing http://www.catholic-hierarchy.org/diocese/lay.html Parsing http://www.catholic-hierarchy.org/diocese/laz.html Name URL 0 Holy See http://www.catholic-hierarchy.org/diocese/droma.html 1 Diocese of Rome http://www.catholic-hierarchy.org/diocese/droma.html 2 Aachen http://www.catholic-hierarchy.org/diocese/da549.html 3 Aachen http://www.catholic-hierarchy.org/diocese/daach.html 4 Aarhus (Århus) http://www.catholic-hierarchy.org/diocese/da566.html 5 Aba http://www.catholic-hierarchy.org/diocese/dabaa.html 6 Abaetetuba http://www.catholic-hierarchy.org/diocese/dabae.html 8 Abakaliki http://www.catholic-hierarchy.org/diocese/dabak.html 9 Abancay http://www.catholic-hierarchy.org/diocese/daban.html 10 Abaradira http://www.catholic-hierarchy.org/diocese/d2a01.html
以上是美麗的湯解析許多條目的列表並保存在資料框中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

在Python中,如何通過字符串動態創建對象並調用其方法?這是一個常見的編程需求,尤其在需要根據配置或運行...
