Python爬虫代码示例:抓取网站数据
以下是一个简单的爬虫代码示例,可以爬取和上述数据相同的数据:
import requests
from bs4 import BeautifulSoup
# 定义爬取函数
def crawl_data(url):
# 发送HTTP请求
response = requests.get(url)
# 解析HTML内容
soup = BeautifulSoup(response.text, 'html.parser')
# 提取需要的数据
data = soup.find_all('a')
for item in data:
print(item.get('href'))
# 调用爬取函数
crawl_data('http://www.example.com')
请注意,这只是一个示例代码,你需要根据实际情况进行适当修改和调整。另外,对于实际的网站爬取,你可能需要了解网站的robots.txt文件和爬虫规则,以避免违反网站的规定。
原文地址: https://www.cveoy.top/t/topic/o6E7 著作权归作者所有。请勿转载和采集!