无聊写了个豆瓣日记的小爬虫,requests+bs4。

cookies_src可填可不填,主要是为了爬取仅自己可见的日记。

url填写的是日记页面,即https://www.douban.com/people/***/notes

import requests
import re
from bs4 import BeautifulSoup # cookies
cookies_src='' # 日记页面
url='' def parse_cookies(str):
str_tmp=str.replace(' ','')
str_list=str.split(';')
cookies={}
for item in str_list:
item_list=item.split('=')
key=item_list[0]
value=item_list[1]
cookies[key]=value
return cookies def get_html(url,cookies):
r=requests.get(url,cookies=cookies)
return r.text # 解析日记页面,提取出标题,时间,内容
def parse_diary(src):
soup=BeautifulSoup(web_html,'html.parser')
note_container=soup.find('div','note-container')
title=note_container.find('div','note-header note-header-container').h1.text
time=note_container.find('span','pub-date').text
content=''
contents=note_container.find('div',id='link-report')
text=str(contents).replace('<br>','\n')
text=text.replace('<p>','')
text=text.replace('</p>','\n')
text=text.replace('<div class="note" id="link-report">','')
text=text.replace('</div>','')
content=content+text
return (title,time,content) cookies=parse_cookies(cookies_src) # 提取处所有日记链接
next_page=url
diarys_link=[]
page_num=1
while True:
print("正在获取第%d页的日记链接..."%page_num)
web_html=get_html(next_page,cookies)
soup=BeautifulSoup(web_html,'html.parser')
# 将当前页面的日记链接保存于diarys_link中
for rr in soup.find_all('div','rr'):
diarys_link.append(rr.a['href'])
try: # 到达最后一页
next_page=soup.find('span','next').a['href']
except:
break
page_num=page_num+1 # 解析每条日记
print('逐条解析日记...')
diarys=[]
num=1
for diary in diarys_link:
web_html=get_html(diary,cookies)
diarys.append(parse_diary(web_html))
print('已解析:%d'%num)
num=num+1 # 写入文件
print('写入文件中...')
with open('./diary.txt','w') as f:
for diary in diarys:
f.write(diary[0])
f.write('\n')
f.write(diary[1])
f.write('\n')
f.write(diary[2])
f.write('\n---------------------------------\n') print('写入成功')

关于日记内容的处理部分,本来是可以直接用.text来获取的,但<br><p>标签均被过滤掉,原文中的换行符就没了,所以只能转换成str再进行替换了。

Python 豆瓣日记爬取的更多相关文章

  1. Python 豆瓣mv爬取

    爬取网址:https://www.dbmeinv.com/       豆瓣mv(现已更名) 注:自制力不好的同学,先去准备营养快线! import requests from bs4 import ...

  2. python豆瓣250爬取

    import requests from bs4 import BeautifulSoup from lxml import etree # qianxiao996精心制作 #博客地址:https:/ ...

  3. Python登录豆瓣并爬取影评

    上一篇我们讲过Cookie相关的知识,了解到Cookie是为了交互式web而诞生的,它主要用于以下三个方面: 会话状态管理(如用户登录状态.购物车.游戏分数或其它需要记录的信息) 个性化设置(如用户自 ...

  4. Python爬虫之爬取慕课网课程评分

    BS是什么? BeautifulSoup是一个基于标签的文本解析工具.可以根据标签提取想要的内容,很适合处理html和xml这类语言文本.如果你希望了解更多关于BS的介绍和用法,请看Beautiful ...

  5. Python爬虫之爬取淘女郎照片示例详解

    这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...

  6. [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】

    [python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...

  7. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  8. 如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)

    前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往下继续深入. 一.代码实现 1.修改Scrapy项目中的items.py ...

  9. from appium import webdriver 使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)

    使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium) - 北平吴彦祖 - 博客园 https://www.cnblogs.com/stevenshushu/p ...

随机推荐

  1. SAP work process Memory allocate

    Memory allocation sequence to dialog work processes in SAP What is the memory allocation sequence to ...

  2. myeclipse内存调整

    内存调整: myeclipse.ini里配置后 1.设置Default VM Arguments 在myEclipse中,打开Windows-> Preferences->Java-> ...

  3. Jenkins 之邮件配置

    Jenkins 之邮件配置其实还是有些麻烦的,坑比较多,一不小心就...我是走了很多弯路的. 这里记录下来,希望大家以后不要重蹈覆辙: 我测试过,这里的 Extended E-mail Notific ...

  4. C#USB设备枚举Kernel32的PInvoke

    using System; using System.Runtime.InteropServices; using System.Security; namespace Splash { #regio ...

  5. keepalived nginx 双机热备图文讲解

    http://blog.csdn.net/wanglei_storage/article/details/51175418

  6. 02.设计模式_NullObject模式

    使用NULL OBJECT模式,我们可以确保返回的总是有效的对象,即使失败时也代表对象什么也不做. 下面以一个数据库查询的示例来演示空对象模式. 1.Employe实体对象空对象的接口 Employe ...

  7. leetcode986

    class Solution: def judgeIntersection(self,a:'Interval',b:'Interval'): #返回值1,bool类型,是否有交集:True-有交集,F ...

  8. react-native 插件汇总

    部分自己搜集 部分 来自别的网站 第三方路由插件 react-native-router-flux react-native-scrollable-tab-view 选项卡 测滑动菜单 react-n ...

  9. CSS VISUAL RULES

    CSS VISUAL RULES Review Visual Rules Incredible work! You used CSS to alter text and images througho ...

  10. 红警2在Y460和win10下运行

    1.将电源模式改为高性能模式 2.以WinXP兼容模式运行 3.修改RA2.ini文件 在[Video]下加入以下代码 : AllowHiResModes=yes VideoBackBuffer=no ...