Python可以使用多種庫來抓取網(wǎng)頁數(shù)據(jù),最常用的是requests和BeautifulSoup庫。
import requests
url = "http://example.com"
response = requests.get(url)
# 檢查請求是否成功
if response.status_code == 200:
# 打印網(wǎng)頁內(nèi)容
print(response.text)
from bs4 import BeautifulSoup
# 假設(shè)已經(jīng)使用requests庫獲取了網(wǎng)頁內(nèi)容,存儲在response變量中
soup = BeautifulSoup(response.text, "html.parser")
# 使用BeautifulSoup提供的方法來提取數(shù)據(jù)
# 例如,提取所有<a>標(biāo)簽中的鏈接
links = soup.find_all("a")
for link in links:
print(link.get("href"))
請注意,具體的抓取方法會根據(jù)網(wǎng)頁的結(jié)構(gòu)和數(shù)據(jù)的位置而有所不同。有時候可能還需要處理一些網(wǎng)頁渲染或動態(tài)加載的問題,可以使用selenium庫來模擬瀏覽器行為。