python 爬取懒人听书
时间: 2023-09-22 11:12:40 浏览: 102
你可以使用 Python 中的爬虫库来爬取懒人听书网站的内容。首先,你需要安装相关的库,比如 requests 和 BeautifulSoup。然后,你可以使用 requests 库发送 HTTP GET 请求来获取网页内容,再使用 BeautifulSoup 解析网页并提取所需的信息。
以下是一个简单的示例代码,演示如何爬取懒人听书网站上的书籍列表:
```python
import requests
from bs4 import BeautifulSoup
url = "https://www.lrts.me/book/category/1"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
books = soup.find_all("div", class_="book-info")
for book in books:
title = book.find("h3").text.strip()
author = book.find("p", class_="author").text.strip()
intro = book.find("div", class_="intro").text.strip()
print("书名:", title)
print("作者:", author)
print("简介:", intro)
print()
```
上述代码中,我们首先使用 requests 库发送 GET 请求获取网页内容,并将其传递给 BeautifulSoup 对象进行解析。然后,我们使用 find_all 方法找到所有带有 class 为 "book-info" 的 div 元素,表示每本书的信息。接着,我们使用 find 方法从这些 div 元素中找到书名、作者和简介等信息,并打印出来。
当然,具体的爬取方式可能会因为网站的结构变化而有所不同,请根据实际情况进行调整。另外,爬取网站内容时要遵守网站的爬虫规则,并尊重网站的使用条款。