生活随笔
收集整理的這篇文章主要介紹了
爬虫实现csdn文章一键(批量)更换阅读类型(全部可见、粉丝可见、vip可见)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
前言
- 在IT首屈一指的交流平臺上,我們可以看得到csdn在最近的一些進步和變化:如blink,文章收益,等等方面。促進和提升文章價值,激發(fā)筆者寫作分享!這無疑是一件好事。
- 但csdn依然還有很多不夠完善或者需要優(yōu)化的地方,如推薦算法、新出的收益無法一鍵更改文章閱讀類型。這讓一些大的博主或者干貨很多的博主(成百上千文章)很難有精力一個一個手動修改、維護自己權(quán)益。
- 作為社會新青年、IT從事者。我們清楚公作人員每天為我們服務很忙,不一定照顧到所有群體。筆者本著樂于助人的精神,故自己動手,寫個腳本,幫助大家解決心理生理難題!
- 該方案針對markdown用戶。富文本可參考類推。
- 功能上分為直接全部更改和分類更改,分類更改需要多輸入一個分類進去的首頁url。其他一致!按照提升即可。
- 如有問題可以聯(lián)系作者!
分析
- 需求既然有了,那么技術上怎么實現(xiàn)呢?
- 我們以前徹底的分析過csdn的登錄機制,發(fā)現(xiàn)csdn很容易登錄的。那么我們就可以用著登錄的cookie進行我們想要的操作。
獲取文章鏈接、id
我們要找到自己站點的所有文章的url和id。因為我們可能會根據(jù)文章id進行操作。
思路:
- 從登錄的cookie種找到你的id,進入csdn主頁。
- 解析主頁的信息獲取頁數(shù),這里說一下。他的頁數(shù)是js動態(tài)加載進去的,并不是直接渲染,如果非得頭鐵debug找也沒問題。但是頁數(shù)和總文章數(shù)有關系。因為一頁最多只能有20篇文章!解析這些,你就能獲取所有文章鏈接、id。
分析markdown文本
- 對任意一個文章、檢查編輯。查看元素獲取下來鏈接。你會發(fā)現(xiàn)鏈接是有規(guī)律的。跟文章id有關。
- 進入之后,你會發(fā)現(xiàn)這個是md好像提不出什么信息。點擊提交看看ajax請求把。
- 這些參數(shù)沒加密。都是原文。我想這個md文件csdn怎么提取。還能根據(jù)h5規(guī)則反向提取?csdn沒那么強吧。肯定有其他方案。仔細觀察發(fā)現(xiàn)加載時候有個xhr文件有了所有信息。我們只需要進行修改部分即可。
代碼編寫
依賴外部包:bs4、requests
python代碼為(沒用多進程,簡單用了下多線程,執(zhí)行完手動結(jié)束關閉)
import requests
from bs4
import BeautifulSoup
import json
import threading
from queue
import Queue
queue
= Queue
()
header
={'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36','referer':'https://passport.csdn.net/login','origin':'https://passport.csdn.net','content-Type':'application/json;charset=UTF-8','x-requested-with':'XMLHttpRequest','accept':'application/json, text/plain, */*','accept-encoding':'gzip, deflate, br','accept-language':'zh-CN,zh;q=0.9','connection': 'keep-alive','Host': 'passport.csdn.net'}
data
={"loginType":"1","pwdOrVerifyCode":"",' \
'"userIdentification":"","uaToken":"",' \'
"webUmidToken":""}
cookies
=""
type='public'
def login(usename
,password
):global cookies
global dataloginurl
= 'https://passport.csdn.net/v1/register/pc/login/doLogin'data
['userIdentification']=usenamedata
['pwdOrVerifyCode']=passworddata
=str(data
)print(data
)req
= requests
.post
(loginurl
, data
=data
, headers
=header
)cookies
= requests
.utils
.dict_from_cookiejar
(req
.cookies
)res
= req
.text
print(req
.status_code
)print(cookies
)url
="https://blog.csdn.net/"+str(cookies
['UN'])return url
def addurl(url
):req2
= requests
.get
(url
, cookies
=cookies
)soup
= BeautifulSoup
(req2
.text
, 'lxml')pagetotal
= soup
.select
(".text-center")[0].get
("title")pagetotal
= (int)(((int)(pagetotal
) + 19) / 20);print(pagetotal
)for index
in range(pagetotal
):url2
= url
+"/article/list/" + str(index
+ 1)print(url2
)req
= requests
.get
(url2
, cookies
=cookies
)soup
= BeautifulSoup
(req
.text
, 'lxml')pages
= soup
.find
(id='mainBox').find_all
(attrs
={'class': 'article-item-box'})for page
in pages
:try:href
= page
.find
("a").get
("href")id = href
.split
("/")id = id[len(id) - 1]print(href
, id)queue
.put
(id)except Exception
as e
:print(e
)
def addurl_by_type(url
):req2
= requests
.get
(url
, cookies
=cookies
)soup
= BeautifulSoup
(req2
.text
, 'lxml')pagetotal
= soup
.select
(".text-center")[0].get
("title")pagetotal
= (int)(((int)(pagetotal
) + 19) / 20);print(pagetotal
)for index
in range(pagetotal
):url2
= url
+ "/" + str(index
+ 1)+"?"print(url2
)req
= requests
.get
(url2
, cookies
=cookies
)soup
= BeautifulSoup
(req
.text
, 'lxml')pages
= soup
.find
(id='mainBox').find_all
(attrs
={'class': 'article-item-box'})for page
in pages
:try:href
= page
.find
("a").get
("href")id = href
.split
("/")id = id[len(id) - 1]print(href
, id)queue
.put
(id)except Exception
as e
:print(e
)def change(id):global read_needTypeurl3
= "https://mp.csdn.net/mdeditor/" + str(id) + "#"url
= "https://mp.csdn.net/mdeditor/getArticle?id=" + str(id)req
= requests
.get
(url
, cookies
=cookies
)res
= req
.json
()data
= res
['data']print(res
)data
['readType'] = read_needTypeurl
= "https://mp.csdn.net/mdeditor/saveArticle"req
= requests
.post
(url
, cookies
=cookies
, data
=data
)res
= req
.text
class downspider(threading
.Thread
):def __init__(self
, threadname
, que
):threading
.Thread
.__init__
(self
)self
.threadname
= threadnameself
.que
= que
def run(self
):print('start thread' + self
.threadname
)while True:try:print(self
.name
,end
='')id=queue
.get
()change
(id)except Exception
as e
:print(e
)breakif __name__
== '__main__':url
=""threads
=[]read_needType
=['public','read_need_fans','read_need_vip']name
=input("name:")password
=input("password:")print("type:\n1:全部可看 \n2關注可看 \n3vip會員可看")value
=input("請輸入數(shù)字")value
=int(value
)-1read_needType
=read_needType
[value
]print("type:\n1:全部更改 \n2更改一個分類")all_or_type
=input("輸入更改范圍(數(shù)字)")all_or_type
=int(all_or_type
)if all_or_type
==1:url
=login
(name
,password
)addurl
(url
)else:print("輸入分類首頁url:")url
=input("url:")login
(name
,password
)addurl_by_type
(url
)print(url
)threadList
= ['thread-1', 'thread-2', 'thread-3', 'thread-4', 'thread-5']for j
in threadList
:thread
= downspider
(j
, queue
)thread
.start
()threads
.append
(thread
)for t
in threads
:t
.join
()
執(zhí)行測試
- 執(zhí)行:有一鍵所有和分類欄目,根據(jù)提示輸入即可!
說在后面的話
-
謹慎使用,杜絕修改,該崩了、錯了文章可能全部完蛋!
-
僅僅為了方便一些博主操作,正確使用自己權(quán)益。
-
如果有效請務必留言點贊,給廣大朋友一個安慰!
-
因為時間原因,白天復習,時間有限,如果有需要java多線程版如果人多可以留言明天補上。夜深了,凌晨1.30.該睡了。
-
謝謝各位閱讀看完支持。
-
如果對后端、爬蟲、數(shù)據(jù)結(jié)構(gòu)算法等感性趣歡迎關注我的個人公眾號交流:bigsai
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的爬虫实现csdn文章一键(批量)更换阅读类型(全部可见、粉丝可见、vip可见)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。