Python 静态网页爬虫代码示例
以下是一个简单的静态网页 Python 爬虫代码示例:
import requests
from bs4 import BeautifulSoup
url = 'http://example.com' # 要爬取的网页URL
# 发送GET请求获取网页内容
response = requests.get(url)
# 使用BeautifulSoup库解析网页内容
soup = BeautifulSoup(response.content, 'html.parser')
# 找到网页中的所有链接
links = soup.find_all('a')
# 输出所有链接的href属性值
for link in links:
print(link.get('href'))
这个爬虫代码使用 requests 库发送 GET 请求获取网页内容,然后使用 BeautifulSoup 库解析网页内容,找到所有链接并输出它们的 href 属性值。你可以根据自己的需求修改这个代码,比如修改要爬取的网页 URL,或者修改解析网页内容的方式。
原文地址: https://www.cveoy.top/t/topic/oYql 著作权归作者所有。请勿转载和采集!