爬虫(Spider)是一种用于自动抓取网站信息的程序。下面是一个简单的 Python 爬虫的例子:
import requests
def crawl(url):
# 发送 HTTP 请求
response = requests.get(url)
# 获取网页内容
html = response.text
# 处理网页内容
# ...
# 开始爬取
crawl('http://www.example.com')
上面的代码使用了 requests
库来发送 HTTP 请求,并获取响应内容。然后,我们就可以对网页内容进行处理了。