【python爬虫案例】利用python爬虫爬取豆瓣读书TOP250的数据! 马哥python说
时间: 2023-10-03 18:04:34 浏览: 322
要利用Python爬虫爬取豆瓣读书TOP250的数据,可以按照以下步骤进行:
- 导入所需的库,包括requests、BeautifulSoup等。
- 构建URL地址,可以使用豆瓣读书的搜索链接,例如"https://book.douban.com/top250"。
- 发起HTTP请求,获取页面的HTML内容。
- 使用BeautifulSoup解析HTML内容,提取出需要的数据,如书名、作者、评分等。
- 将提取出的数据保存到一个文件或数据库中,以便进一步分析和使用。
下面是一个简单的示例代码,可以帮助你开始爬取豆瓣读书TOP250的数据:
```python import requests from bs4 import BeautifulSoup
def crawl_douban_top250(): url = "https://book.douban.com/top250" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36" } response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser')
books = soup.find_all('tr', class_="item")
for book in books:
title = book.find('div', class_="pl2").find('a').text.strip()
info = book.find('p', class_="pl").text.strip().split('/')
author = info
相关推荐












