以下是一个简单的 Python 爬虫示例,它使用 Requests 库获取网页内容,并使用 BeautifulSoup 解析 HTML 代码,提取页面中的所有链接:

import requests
from bs4 import BeautifulSoup

url = 'https://www.example.com'
response = requests.get(url)

if response.status_code == 200:
    soup = BeautifulSoup(response.content, 'html.parser')
    links = soup.find_all('a')

    for link in links:
        print(link.get('href'))
else:
    print('Error:', response.status_code)

这个爬虫首先使用 requests 模块获取网页内容,然后使用 BeautifulSoup 模块解析 HTML 代码。它查找页面中的所有链接,并打印它们的 URL。请注意,这个示例不包括错误处理或其他高级功能,但它可以作为入门级爬虫的起点。

Python 爬虫入门:使用 Requests 和 BeautifulSoup 抓取网页链接

原文地址: https://www.cveoy.top/t/topic/mAHl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录