项目地址:
用到了beautifulsoup4,请先安装。
pip install beautifulsoup4
开心网日记爬取
kaixin001.py
使用
登录开心网,浏览器f12看http请求的header,获取自己的cookie。
填写cookie,要爬的日记的url,要爬的总次数。走你。
之后会生成html文件,格式是<:title>-<yyyymmddhhmmss>
代码
# -*- coding: utf-8 -*- from urllib.request import urlopen import urllib.request import urllib.parse #为了获取http response from bs4 import beautifulsoup #bs4 import string # 为了去掉空白字符 import time # 防止被杀cookie import unicodedata # 字符修正 # 在这里放第一个链接 urlx = '链接' #写你想爬的文 def request(url): global urlx #引用外面的链接作为全局变量,后面还会取下一个进行循环的 # 使用urllib库提交cookie获取http响应 headers = { 'get https':url, 'host':' www.kaixin001.com', 'connection':' keep-alive', 'upgrade-insecure-requests':' 1', 'user-agent':' mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/90.0.4430.212 safari/537.36', 'accept':' application/json, text/javascript, */*; q=0.01', 'accept-language':' zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7', 'cookie':' ', #改成自己的cookie,自己浏览器打开网站f12调试,自己找http请求的header } request = urllib.request.request(url=url,headers=headers) response = urllib.request.urlopen(request) contents = response.read() # 使用bs4获得所有htmltag bsobj = beautifulsoup(contents,"html.parser") # 使用bs4的find函数得到想要的东西:标题、发表时间和博客正文 title = bsobj.find("b", attrs={"class":"f14"}) titlet = bsobj.find("b", attrs={"class":"f14"}).get_text() #开心网日记的标题是一个b标签,class属性值是f14 date = bsobj.find("span", attrs={"class":"c6"}) datet = bsobj.find("span", attrs={"class":"c6"}).get_text() #开心网日记的发表时间是一个span标签,class属性值是c6 text = bsobj.find("div", attrs={"class":"textcont"}) textt = bsobj.find("div", attrs={"class":"textcont"}).get_text() #开心网日记的正文是一个div标签,class属性值是textcont # 测试输出 print(title) print(datet) # print(text) # 生成html文件。这里直接用file.open()和file.write()了,也可以用jinja2之类的框架生成。 remove = string.whitespace+string.punctuation table = str.maketrans(':',':',remove) filetitle=str(titlet).replace(':',':').replace('''"''','''“''')+'-'+str(datet).translate(table).replace('发表','')+'.html' print(filetitle) #测试输出 f = open(filetitle,'w',encoding="utf-8") #注意用utf-8编码写入,不然会因为一些旧博文采用的gbk编码不兼容而出问题。 # 写入message message = """ <html> <head></head> <body> <h1>%s</h1> <b>%s</b> <br></br> %s </body> </html>"""%(title.get_text(),date.get_text(),unicodedata.normalize('nfd',text.prettify())) f.write(message) f.close() # webbrowser.open(filetitle,new = 1) # 定位下一篇博文的url nexturl=bsobj.find("a",text="下一篇 >").attrs["href"] #下一篇是一个a标签,使用tag对象的attrs属性取href属性的值。开心网的日记系统里,如果到了最后一篇日记,下一篇的链接内容是第一篇日记,所以不用担心从哪篇日记开始爬。 # print(nexturl) urlx="http://www.kaixin001.com"+nexturl print(urlx) # 主循环,给爷爬 num=328 #设定要爬多少次。其实也可以写个数组检测重复然后中止的啦,但我懒得弄了。 for a in range(num): request(urlx) print('we get '+str(a+1)+' in '+str(num)) time.sleep(1) # 慢点,慢点。测试过程中出现了没有设置限制爬一半cookie失效了的情况,可能是太快了被搞了。
豆瓣日记爬取
douban.py
使用
登录豆瓣,浏览器f12看http请求的header,获取自己的cookie。
填写变量cookie,要爬的日记页的url。走你。
之后会生成html文件,格式是<:title>-<yyyymmddhhmmss>
代码
# -*- coding: utf-8 -*- from urllib.request import urlopen import urllib.request import urllib.parse #为了获取http response from bs4 import beautifulsoup #bs4 import string # 为了去掉空白字符 import unicodedata # 字符修正 import re # 在这里放链接 url = '' #写你想爬的人 https://www.douban.com/people/xxx/notes 这样 cookie = '' def request(urlx): global url #引用外面的链接作为全局变量,后面还会取下一个进行循环的 global boolean global cookie # 使用urllib库提交cookie获取http响应 headers = { 'get https':urlx, 'host':' www.douban.com', 'connection':' keep-alive', 'upgrade-insecure-requests':' 1', 'user-agent':' mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/90.0.4430.212 safari/537.36', 'accept':' application/json, text/javascript, */*; q=0.01', 'accept-language':' zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7', 'cookie':cookie, #改成自己的cookie,自己浏览器打开网站f12调试,自己找http请求的header } request = urllib.request.request(url=urlx,headers=headers) response = urllib.request.urlopen(request) contents = response.read() # 使用bs4获得所有htmltag bsobj = beautifulsoup(contents,"html.parser") # 使用bs4的find函数获取当前页面的所有日记链接 article = bsobj.find("div", attrs={"class":"article"}) titleset = article.findall("h3") # print(titleset) for title in titleset: titletext = title.findall("a",attrs={"class":"j a_unfolder_n"}) for link in titletext: noteurl = str(link.attrs["href"]) print(noteurl) requestsinglepage(noteurl) next = bsobj.find("a",text="后页>") if next==none: print("结束了") boolean=1 else: url = str(next.attrs["href"]).replace("&type=note","") print(url) def requestsinglepage(urly): global cookie headers = { 'get https':urly, 'host':' www.douban.com', 'connection':' keep-alive', 'upgrade-insecure-requests':' 1', 'user-agent':' mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/90.0.4430.212 safari/537.36', 'accept':' application/json, text/javascript, */*; q=0.01', 'accept-language':' zh-cn,zh;q=0.9,en-us;q=0.8,en;q=0.7', 'cookie':cookie, #改成自己的cookie,自己浏览器打开网站f12调试,自己找http请求的header } request = urllib.request.request(url=urly,headers=headers) response = urllib.request.urlopen(request) contents = response.read() # 使用bs4获得所有htmltag bsobj = beautifulsoup(contents,"html.parser") # 使用bs4的find函数得到想要的东西:标题、发表时间和博客正文 title = bsobj.find("h1").get_text() date = bsobj.find("span", attrs={"class":"pub-date"}) datet = bsobj.find("span", attrs={"class":"pub-date"}).get_text() text = bsobj.find("div", attrs={"id":"link-report"}) # textt = bsobj.find("div", attrs={"class":"textcont"}).get_text() # 测试输出 print(title) print(datet) # 生成html文件。这里直接用file.open()和file.write()了,也可以用jinja2之类的框架生成。 remove = string.whitespace+string.punctuation # 去掉日期的标点符号 table = str.maketrans(':',':',remove) filetitle=str(title)+'-'+str(datet).translate(table)+'.html' print(filetitle) #测试输出 f = open(filetitle,'w',encoding="utf-8") #注意用utf-8编码写入,不然会因为一些旧博文采用的gbk编码不兼容而出问题。 # 写入message message = """ <html> <head></head> <body> <h1>%s</h1> <b>%s</b> <br></br> %s </body> </html>"""%(title,datet,unicodedata.normalize('nfd',text.prettify())) f.write(message) f.close() # 主循环,给爷爬 boolean=0 while(boolean==0): a=1 request(url) print('we finished page '+str(a)+' .') a+=1
roadmap
豆瓣四月份时候还有bug,手机端可以看到全部日记,半年隐藏无效。最近修好了。
不过现在的隐藏依然没有针对到具体的日记,或许可以想办法通过其他手段爬下来。
以上就是python 开心网日记爬取的示例步骤的详细内容,更多关于python 开心网日记爬取的资料请关注www.887551.com其它相关文章!
黄山市民网:https://www.huangshanshimin.com/