python爬虫豆瓣图书
时间: 2023-11-08 14:51:21 浏览: 51
好的,可以使用Python的requests和BeautifulSoup库来爬取豆瓣图书的信息。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://book.douban.com/top250"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
books = soup.find_all("div", class_="pl2")
for book in books:
title = book.a["title"]
link = book.a["href"]
rating = book.find("span", class_="rating_nums").text
author = book.find("span", class_="pl").text.strip().split("/")[0]
print("书名:", title)
print("链接:", link)
print("评分:", rating)
print("作者:", author)
print("\n")
```
这个代码会爬取豆瓣图书排行榜前250本书的书名、链接、评分和作者,并输出到控制台。你可以根据自己的需要对代码进行修改和优化。需要注意的是,爬虫的过程需要遵守网站的规则和法律法规,不要进行恶意爬取。
阅读全文