使用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从网页中抓取数据并提取出需要的内容。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 怎么使用BeautifulSoup处理多语言网页