使用BeautifulSoup從網頁中抓取數據的步驟如下:
from bs4 import BeautifulSoup
import requests
url = 'https://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到所有的標題
titles = soup.find_all('h2')
# 找到所有的鏈接
links = soup.find_all('a')
# 找到特定class的元素
specific_class = soup.find_all(class_='specific-class')
for title in titles:
print(title.text)
for link in links:
print(link['href'])
for element in specific_class:
print(element.text)
通過以上步驟,您可以使用BeautifulSoup從網頁中抓取數據并提取出需要的內容。