# -*- coding: utf-8 -*-

import requests
import re, json

if __name__ == '__main__':
    content = requests.get('https://book.douban.com/').text
    reg_base = '<ul.*?list-col list-col5 list-express slide-item">(.*?)</ul>'
    base_pattern = re.compile(reg_base, re.S)
    base_html = re.findall(base_pattern, content)

href = '<li.*?cover.*?href="(.*?)".*?'
    title = '<div.*?title.*?title.*?>(.*?)</a>.*?'
    author = '<div.*?more-meta.*?author.*?>(.*?)</span>.*?</li>'
    regex = href + title + author
    pattern = re.compile(regex, re.S)
    results = []
    # 匹配子标签中的图书信息
    for html in base_html:
        results += re.findall(pattern, html)

with open('touban.txt', 'w', encoding='utf-8') as f:
        for result in results:
            d = {
                'href': result[0].strip(),
                'title': result[1].strip(),
                'author': result[2].replace('&nbsp;', '').strip()
            }

f.write(json.dumps(d, ensure_ascii=False) + '\n')

{"author": "[法] 米歇尔·普西", "href": "https://book.douban.com/subject/30180673/?icn=index-editionrecommend", "title": "她不是我妈妈"}
{"author": "[意]马西米利亚诺·威尔吉利奥", "href": "https://book.douban.com/subject/30180821/?icn=index-editionrecommend", "title": "那不勒斯的萤火"}
{"author": "于蕾,吕逸涛", "href": "https://book.douban.com/subject/30206904/?icn=index-editionrecommend", "title": "国家宝藏"}
{"author": "张立民", "href": "https://book.douban.com/subject/30235899/?icn=index-editionrecommend", "title": "最后一公里的哲学:电商物流全链条运营管理"}
{"author": "【英】詹姆斯•霍尼伯内(James Honeyborne)/【英】马克•布朗罗(Mark Brownlow)", "href": "https://book.douban.com/subject/30183403/?icn=index-editionrecommend", "title": "BBC全新4K海洋百科:蓝色星球II"}
{"author": "[葡] 若泽·萨拉马戈", "href": "https://book.douban.com/subject/27598520/?icn=index-latestbook-subject", "title": "里卡尔多·雷耶斯离世那年"}
{"author": "[美] 史蒂芬·平克", "href": "https://book.douban.com/subject/30186025/?icn=index-latestbook-subject", "title": "风格感觉"}
{"author": "赵垒", "href": "https://book.douban.com/subject/30204837/?icn=index-latestbook-subject", "title": "傀儡城之荆轲刺秦"}
{"author": "梅贻琦/黄延复/王小宁", "href": "https://book.douban.com/subject/30197575/?icn=index-latestbook-subject", "title": "梅贻琦西南联大日记"}
{"author": "[日] 永井荷风", "href": "https://book.douban.com/subject/30171301/?icn=index-latestbook-subject", "title": "濹东绮谭"}
{"author": "[波兰] 安杰伊·瓦伊达/Andrzej Wajda", "href": "https://book.douban.com/subject/30211002/?icn=index-latestbook-subject", "title": "我们一起拍片!"}
{"author": "[德] 弗兰克·施茨廷", "href": "https://book.douban.com/subject/27604676/?icn=index-latestbook-subject", "title": "群"}
{"author": "[美] 克丽丝特尔·潘恩/Crystal Paine", "href": "https://book.douban.com/subject/30206819/?icn=index-latestbook-subject", "title": "会赚钱的妈妈"}
{"author": "[日] 石田衣良", "href": "https://book.douban.com/subject/27622428/?icn=index-latestbook-subject", "title": "美丽的孩子"}
{"author": "杨时旸", "href": "https://book.douban.com/subject/30218577/?icn=index-latestbook-subject", "title": "孤独的影猎人"}
{"author": "[德]沃尔夫冈·赫伦多夫", "href": "https://book.douban.com/subject/27598521/?icn=index-latestbook-subject", "title": "小心,沙漠有人"}
{"author": "[英] 珍妮特·温特森", "href": "https://book.douban.com/subject/27663541/?icn=index-latestbook-subject", "title": "我要快乐,不必正常"}
{"author": "朱一叶", "href": "https://book.douban.com/subject/30198364/?icn=index-latestbook-subject", "title": "死于象蹄"}
{"author": "[荷] 伊恩·布鲁玛", "href": "https://book.douban.com/subject/27662697/?icn=index-latestbook-subject", "title": "日本之镜"}
{"author": "[美] 威廉·莫尔顿·马斯顿", "href": "https://book.douban.com/subject/30210732/?icn=index-latestbook-subject", "title": "神奇女侠"}
{"author": "[美] 特德·焦亚", "href": "https://book.douban.com/subject/30203912/?icn=index-latestbook-subject", "title": "如何听爵士"}
{"author": "邓安庆", "href": "https://book.douban.com/subject/30221630/?icn=index-latestbook-subject", "title": "纸上王国"}
{"author": "朱伟", "href": "https://book.douban.com/subject/30205589/?icn=index-latestbook-subject", "title": "重读八十年代"}
{"author": "邓安庆", "href": "https://book.douban.com/subject/30190319/?icn=index-latestbook-subject", "title": "望花"}
{"author": "[美]沃尔特·李普曼", "href": "https://book.douban.com/subject/27662713/?icn=index-latestbook-subject", "title": "舆论"}
{"author": "[英] P•D•詹姆斯", "href": "https://book.douban.com/subject/27111572/?icn=index-latestbook-subject", "title": "人类之子"}
{"author": "骆仪", "href": "https://book.douban.com/subject/30198500/?icn=index-latestbook-subject", "title": "京都好物"}
{"author": "(美) 比尔·克林顿 (Bill Clinton)/[美] 詹姆斯·帕特森", "href": "https://book.douban.com/subject/30218923/?icn=index-latestbook-subject", "title": "失踪的总统"}
{"author": "刘冰/林秦文/李敏", "href": "https://book.douban.com/subject/30203973/?icn=index-latestbook-subject", "title": "中国常见植物野外识别手册(北京册)"}
{"author": "冶文彪", "href": "https://book.douban.com/subject/30205286/?icn=index-latestbook-subject", "title": "清明上河图密码 5"}
{"author": "[英] 劳拉·卡琳/Laura Carlin", "href": "https://book.douban.com/subject/30181220/?icn=index-latestbook-subject", "title": "创造自己的世界"}
{"author": "郭强生", "href": "https://book.douban.com/subject/30217599/?icn=index-latestbook-subject", "title": "断代"}
{"author": "史杰鹏", "href": "https://book.douban.com/subject/30183948/?icn=index-latestbook-subject", "title": "悠悠我心"}
{"author": "[俄] 柳德米拉·乌利茨卡娅", "href": "https://book.douban.com/subject/30205823/?icn=index-latestbook-subject", "title": "库科茨基医生的病案"}
{"author": "[美] 兰德尔·柯林斯", "href": "https://book.douban.com/subject/30143236/?icn=index-latestbook-subject", "title": "文凭社会"}
{"author": "[法] 让-皮埃尔·吉布拉", "href": "https://book.douban.com/subject/30205166/?icn=index-latestbook-subject", "title": "爱的缓刑"}
{"author": "[美]丽贝卡·特雷斯特", "href": "https://book.douban.com/subject/30128172/?icn=index-latestbook-subject", "title": "单身女性的时代"}
{"author": "[俄] 弗拉基米尔·索罗金", "href": "https://book.douban.com/subject/27200259/?icn=index-latestbook-subject", "title": "碲钉国"}
{"author": "苏精", "href": "https://book.douban.com/subject/30218894/?icn=index-latestbook-subject", "title": "铸以代刻"}
{"author": "[英] 石黑一雄", "href": "https://book.douban.com/subject/30181685/?icn=index-latestbook-subject", "title": "莫失莫忘"}

requests + re 爬去网站图书信息(Python)相关推荐

  1. 爬取起点网站图书信息(书名、作者、简介、图片url)

    # 爬取qidian网站图书信息(书名.作者.简介.图片url) import requests from lxml import etree import jsonclass BookSpider( ...

  2. Python Scrapy爬虫实战(1):豆瓣网站图书信息案例- Scrapy初体验

    Scrapy,Python开发的一个快速.高层次的屏幕抓取和web抓取框架,用于抓取web站点并从页面中提取结构化的数据.Scrapy用途广泛,可以用于数据挖掘.监测和自动化测试.Scrapy吸引人的 ...

  3. Python爬虫:爬去韩国电视剧信息

    最近看韩剧想重温一下以前看的韩剧 但是就记得剧情 到网站上找了 太多点的太麻烦,网上问了也回答不了 找的几个片名都不对,所以就想写个爬虫爬去某站上所有的韩剧信息,可以方便查找想看的韩剧 爬取具体如下: ...

  4. requests.get()爬去中文网页乱码解决方法

    requests.get()爬去中文网页乱码解决方法 当我们使用requests.get()爬取百度首页时会发现,返回的html代码中的中文发生乱码. import requestsheaders = ...

  5. python爬火车票是不是违法_python利用selenium+requests+beautifulsoup爬取12306火车票信息...

    在高速发展的时代.乘车出远门是必不可少的,有些查询信息是要收费的.这里打造免费获取火车票信息 想要爬取12306火车票信息,访问12306官方网站,输入出发地,目的地  ,时间  之后点击确定,这是我 ...

  6. 提取点位属性文本_手把手教你如何用Python爬取网站文本信息

    提取网页源代码--Requests 工具包 在我们提取网络信息之前,我们必须将网页的源代码进行提取,Requests工具包现在可以说是最好用和最普及的静态网页爬虫工具,它是由大神Kenneth Rei ...

  7. python爬取网页文本_手把手教你如何用Python爬取网站文本信息

    提取网页源代码--Requests 工具包 在我们提取网络信息之前,我们必须将网页的源代码进行提取,Requests工具包现在可以说是最好用和最普及的静态网页爬虫工具,它是由大神Kenneth Rei ...

  8. 【selenium爬虫】 selenium自动化爬取京东图书信息

    一.题目要求:搜索京东图书页,自动点击详情页进入,爬取数据信息并且存储到csv中二.解题思路:1.需要下载一个chromedriver.exe.下载地址:ChromeDriver Mirror 注意: ...

  9. 爬虫实战:Requests+BeautifulSoup 爬取京东内衣信息并导入表格(python)

    准备工作 假如我们想把京东内衣类商品的信息全部保存到本地,通过手工复制粘贴将是一项非常庞大的工程,此时,可以用python爬虫实现. 第一步:分析网页地址 起始网页地址 起始网页地址 https:// ...

最新文章

  1. Python 把列表转成元组
  2. SpringMVC+SwfUpload进行多文件同时上传
  3. 技能树升级——Chrome Headless模式 - 全栈客栈 - SegmentFault
  4. SpringBoot中使用Thymeleaf常用功能(一):表达式访问数据
  5. 银行卡突然收到500万银行会监控吗?
  6. 判断两个时间段是否重叠的算法
  7. 拼多多:永远不会对孵化品牌“二选一” 扶持千家工厂触达4.4亿消费者
  8. 书还没出,已经在chinapub被虐了
  9. 今日头条张一鸣:做CEO要避免理性的自负
  10. java中将zip文件解压到指定目录下
  11. 【致远FAQ】致远OA启动不起来了(上集)
  12. java到达时间后自动执行代码_java设置按时间自动执行
  13. 对SPEA算法的一些总结
  14. 天数最少的年份_农历辛丑年仅有354天是怎么回事 平年比闰年少几天
  15. 养蛙火爆,大数据解读《旅行青蛙》崛起之谜
  16. tm4c123gxl库函数调包侠养成(三)——————外部中断与按键
  17. clickhouse集群搭建
  18. 微软edge浏览器花屏_如何玩Microsoft Edge的秘密冲浪游戏
  19. 女生学计算机真的难吗,女生想学计算机科学与技术,怕很难学懂,这门学科是不是特别难?女生学到底好不好?...
  20. 立体五子棋中的一个有趣的bug

热门文章

  1. R pdf大小_安卓上使用R语言
  2. 华为机试2022试卷 三道题
  3. springboot基于微信小程序的运动软件前端的设计与实现 毕业设计-附源码100932
  4. 硕博研究生 期间应明确的50件事
  5. Mysql怎么把微信昵称(带emoji表情)保存到数据库,亲测可用
  6. Java多线程编程-(6)-两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier
  7. 一种用于加密流分类的多模态深度学习框架A Novel Multimodal Deep Learning Framework for Encrypted Traffic Classification论文
  8. python时间相减_python 计算时间差,时间加减运算代码
  9. 数据库事务(常被问的)
  10. 简述promise原理