PyCharm 简单爬虫代码示例 - 使用 Requests 和 BeautifulSoup
以下是一个使用 PyCharm 编写的简单爬虫代码示例:
import requests
from bs4 import BeautifulSoup
# 发起HTTP请求
response = requests.get('http://example.com')
# 解析HTML内容
soup = BeautifulSoup(response.content, 'html.parser')
# 提取需要的数据
title = soup.title.text
links = soup.find_all('a')
# 打印结果
print('网页标题:', title)
print('所有链接:')
for link in links:
print(link.get('href'))
在这个示例中,我们使用了requests库发送HTTP请求,并使用BeautifulSoup库解析HTML内容。然后,我们提取了网页标题和所有链接,并将结果打印出来。
在 PyCharm 中,你可以通过创建一个新的 Python 文件,将上述代码复制粘贴到文件中,并点击运行按钮来执行代码。
原文地址: https://www.cveoy.top/t/topic/nvc1 著作权归作者所有。请勿转载和采集!