要用 Python 做一个爬虫程序,首先需要学会如何发送 HTTP 请求,以及如何解析 HTML 网页,提取所需信息。
常用的 HTTP 请求库有 Requests,常用的 HTML 解析库有 BeautifulSoup。
以下是一个简单的爬虫程序示例,它爬取了网页标题:
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser