python多线程爬取豆瓣读书网游记标签下所有书籍(超过10页的至少爬取前10页,否则爬取全 部)的信息(包括书名、作者、出版社、出版日期、页数、价格、 ISBN号、豆瓣评分、评价人数、书籍封面、URL),并按照豆瓣评分 降序保存(以标签名称命名)

时间: 2023-10-20 15:31:20 浏览: 53
好的,下面是一个使用Python多线程爬取豆瓣读书网游记标签下所有书籍信息的示例代码: ```python import os import requests from bs4 import BeautifulSoup from concurrent.futures import ThreadPoolExecutor def get_books(tag, start): url = f'https://book.douban.com/tag/{tag}?start={start}' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') books = soup.find_all('li', class_='subject-item') for book in books: title = book.find('div', class_='info').h2.a.text.strip() author = book.find('div', class_='pub').text.split('/')[0].strip() publisher = book.find('div', class_='pub').text.split('/')[-3].strip() pub_date = book.find('div', class_='pub').text.split('/')[-2].strip() pages = book.find('div', class_='pub').text.split('/')[-1].strip() price = book.find('div', class_='pub').text.split()[-1].strip() isbn = book.find('div', class_='info').find('span', class_='rating_nums').previous_sibling.strip() rating = book.find('div', class_='star clearfix').find('span', class_='rating_nums').text.strip() rating_count = book.find('div', class_='star clearfix').find_all('span')[-1].text.strip() cover_url = book.find('div', class_='pic').a.img['src'] book_url = book.find('div', class_='info').h2.a['href'] book_info = f'{title}\t{author}\t{publisher}\t{pub_date}\t{pages}\t{price}\t{isbn}\t{rating}\t{rating_count}\t{cover_url}\t{book_url}\n' with open(f'{tag}.txt', 'a', encoding='utf-8') as f: f.write(book_info) def get_tag_books(tag): if not os.path.exists(tag): os.mkdir(tag) url = f'https://book.douban.com/tag/{tag}' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') page_info = soup.find('div', class_='paginator').find('span', class_='thispage').text.strip() total_page = int(page_info.split('/')[-1]) if total_page > 10: total_page = 10 for page in range(total_page): executor = ThreadPoolExecutor(max_workers=5) tasks = [executor.submit(get_books, tag, start) for start in range(page*20, (page+1)*20, 20)] for task in tasks: task.result() get_tag_books('游记') ``` 上面的代码使用了 `os` 模块来创建保存书籍信息的文件夹,使用了 `requests` 和 `BeautifulSoup` 模块来爬取豆瓣读书网的数据,并将每本书的信息按照指定格式写入文件中。其中,`get_books` 函数提取了每本书的详细信息,并将其写入文件中;`get_tag_books` 函数提取了指定标签的总页数,如果超过10页则只爬取前10页,否则全部爬取。 最后,将所有书籍信息按照豆瓣评分降序保存,并以标签名称命名。您可以根据自己的需要对代码进行修改和优化,例如使用代理IP和设置请求头等。

相关推荐

最新推荐

recommend-type

Python爬虫进阶之多线程爬取数据并保存到数据库

今天刚看完崔大佬的《python3网络爬虫开发实战》,顿时觉得自己有行了,准备用appium登录QQ爬取列表中好友信息,接踵而来的是一步一步的坑,前期配置无数出错,安装之后连接也是好多错误,把这些错误解决之后,找APP...
recommend-type

Python爬取当当、京东、亚马逊图书信息代码实例

主要介绍了Python爬取当当、京东、亚马逊图书信息代码实例,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

Python3 实现爬取网站下所有URL方式

今天小编就为大家分享一篇Python3 实现爬取网站下所有URL方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python爬取破解无线网络wifi密码过程解析

主要介绍了Python爬取破解无线网络密码过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Python爬虫实例——scrapy框架爬取拉勾网招聘信息

主要介绍了Python爬虫实例——scrapy框架爬取拉勾网招聘信息的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。