#!/usr/bin/python
# coding=utf-8
import requests
import re
# 從一個網(wǎng)頁里獲取文章id
def getArticleIDStringFromWebAtUrlString(urlString):
return getIDStringFromWebAtUrlString(urlString, '<a class="title" target="_blank" href="/p/(.*?)">')
# 從一個網(wǎng)頁里獲取專題id
def getTopicIDStringFromWebAtUrlString(urlString):
return getIDStringFromWebAtUrlString(urlString,'<a class="collection-tag" target="_blank" href="/c/(.*?)">')
# 從一個網(wǎng)頁里按正則獲取對應(yīng)的字符串集合
def getIDStringFromWebAtUrlString(urlString,regString):
html = requests.get(urlString)
commentsIDStrings = re.findall(regString,html.text,re.S)
return commentsIDStrings
# 從文章網(wǎng)頁里拿到評論的id
def getComentsIDStringFromArticleUrlString(urlString):
html = requests.get(urlString)
commentsIDStrings = re.findall('data-note-id="(.*?)">',html.text,re.S)
return commentsIDStrings[0]
# 從文章網(wǎng)頁里拿到文章id
def getArticleIDStringFromArticleUrlString(urlString):
prefixStr = 'http://www.reibang.com/p/'
if prefixStr in urlString:
articleIDString = urlString.replace(prefixStr, '')
return articleIDString
return 'this is not a articleUrlString'
# 評論一篇文章,文章地址痹雅,評論的內(nèi)容
def commentArticle(articleUrlString, commentString):
cookieString = 'remember_user_token=W1sxOTI1NjJdLCIkMmEkMTAkeUdIRkRGdFN4L0RpZldKV3lKalR6ZSIsIjE1MDMzMjY2NTMuODY3NTI3Il0%3D--7255eb9eb5c5465a41781ef8e5772b10e1b2d72e; _ga=GA1.2.714896647.1497170140; _gid=GA1.2.462532388.1503150885; Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1503323642,1503323726,1503325746,1503326272; Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1503326790; _session_id=ZkhpL1IzRzA4bEs2dTZRRlhSTlVaQ3JFMWQ0enc4akxtSjh1dlUrSlpCUGpJZGltZG42UDdDMmtodllJbkovZFZ1eEkxVlNjZDUzVG1RdVc1cysyQUZTVUxFZFBUUC9zL3dIZjNVdFJVeERsZWpET2NRK0J3dHFnN21xRkE3QlFuYlBPU1RZc09xTXFJRlBFNGwrU1U3Q0gzQnpTRnNmZXVvNmR1dzJOZ2J0MU5KMkQ4cnd5OTlpYlIyZXRyb21rdjBMeE9xS1lPWGRSL2t0UXlyeDN2bml2TWNwMDF4akpGSkVQaW5iSXBDdWJCNUxrZEJ3dGJDZUhnOU50a2wyV2N2b3g0dXkvd0M5VWkzNTh6eUtPRUdTZUJuUHo2REg1dXhqeGpNOExubm40enU5U3BPNG1xdnlOK0dxYTJFeU5GZVQ3OTlFTW1GdndUS043T0N6QlpGczY4VFJCYW5QTDlxUzRMOGJYN3RyeU05N2cvS0lDWDNidVJZNkgwbmF3aHVMY083TWgrc2RsRHRYVDBjYjIwZVJTdXRHc21MWnk4Z0I3VFdMWHNmOD0tLU8zaHdXcEFUZjNaLzVINWpIdUZnK0E9PQ%3D%3D--4ff5e23579bb725201c91a9bfa6d7bb5975bd076'
ArticleIDString = getArticleIDStringFromArticleUrlString(articleUrlString)
noteIDString = getComentsIDStringFromArticleUrlString(articleUrlString)
data = {"content": commentString}
#header
headers = {'Host':'www.reibang.com',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'Accept':'application/json',
'Accept-Language':'zh-CN,zh;q=0.8',
'Content-Type':'application/json',
'Cookie':cookieString,
'Referer':'http://www.reibang.com/p/%s'%ArticleIDString,
'Origin':'http://www.reibang.com',
'Connection':'keep-alive',
'Accept-Encoding':'gzip, deflate',
'DNT':'1'}
response = requests.post(url='http://www.reibang.com/notes/%s/comments'%noteIDString,headers=headers,json=data)
print response.status_code, response.reason
# 喜歡一個專題里的文章
def likeTopic(topicUrlString):
articleIDStrings = getArticleIDStringFromWebAtUrlString(topicUrlString)
for articleIDString in articleIDStrings:
# 喜歡一個文章跟評論一個文章類似的寫法
likeArticle(articleIDString)
# 喜歡一文章
def likeArticle(articleUrlString):
cookieString = 'remember_user_token=W1sxOTI1NjJdLCIkMmEkMTAkeUdIRkRGdFN4L0RpZldKV3lKalR6ZSIsIjE1MDMzMjY2NTMuODY3NTI3Il0%3D--7255eb9eb5c5465a41781ef8e5772b10e1b2d72e; _ga=GA1.2.714896647.1497170140; _gid=GA1.2.462532388.1503150885; Hm_lvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1503323642,1503323726,1503325746,1503326272; Hm_lpvt_0c0e9d9b1e7d617b3e6842e85b9fb068=1503326790; _session_id=ZkhpL1IzRzA4bEs2dTZRRlhSTlVaQ3JFMWQ0enc4akxtSjh1dlUrSlpCUGpJZGltZG42UDdDMmtodllJbkovZFZ1eEkxVlNjZDUzVG1RdVc1cysyQUZTVUxFZFBUUC9zL3dIZjNVdFJVeERsZWpET2NRK0J3dHFnN21xRkE3QlFuYlBPU1RZc09xTXFJRlBFNGwrU1U3Q0gzQnpTRnNmZXVvNmR1dzJOZ2J0MU5KMkQ4cnd5OTlpYlIyZXRyb21rdjBMeE9xS1lPWGRSL2t0UXlyeDN2bml2TWNwMDF4akpGSkVQaW5iSXBDdWJCNUxrZEJ3dGJDZUhnOU50a2wyV2N2b3g0dXkvd0M5VWkzNTh6eUtPRUdTZUJuUHo2REg1dXhqeGpNOExubm40enU5U3BPNG1xdnlOK0dxYTJFeU5GZVQ3OTlFTW1GdndUS043T0N6QlpGczY4VFJCYW5QTDlxUzRMOGJYN3RyeU05N2cvS0lDWDNidVJZNkgwbmF3aHVMY083TWgrc2RsRHRYVDBjYjIwZVJTdXRHc21MWnk4Z0I3VFdMWHNmOD0tLU8zaHdXcEFUZjNaLzVINWpIdUZnK0E9PQ%3D%3D--4ff5e23579bb725201c91a9bfa6d7bb5975bd076'
prefixStr = 'http://www.reibang.com/p/'
if prefixStr not in articleUrlString:
articleUrlString = prefixStr+articleUrlString
noteIDString = getComentsIDStringFromArticleUrlString(articleUrlString)
data = {}
headers = {'Host':'www.reibang.com',
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',
'Accept':'application/json',
'Accept-Language':'zh-CN,zh;q=0.8',
'Content-Type':'application/json',
'Cookie':cookieString,
'Referer':articleUrlString,
'Origin':'http://www.reibang.com',
'Connection':'keep-alive',
'Accept-Encoding':'gzip, deflate',
'DNT':'1'}
response = requests.post(url='http://www.reibang.com/notes/%s/like'%noteIDString,headers=headers,json=data)
print response.status_code, response.reason
按上面我們會遇到一個問題仰担,就是拿不到一個用戶的所有文章,一個專題的所有文章,解決方案:離線網(wǎng)頁摔蓝,再抓取網(wǎng)頁源碼
# 從本地一個html文件中解析出文章的id
def getArticalIDStringFromLocalHtmlFile(localHtmlFilePath):
with open(r'%s'%localHtmlFilePath, "r") as f:
page = f.read()
commentsIDStrings = re.findall('<a class="title" target="_blank" href="http://www.reibang.com/p/(.*?)">',page,re.S)
return commentsIDStrings
# 這里我們可以開始調(diào)用了赂苗,這里要寫全路徑 不能寫 ~/Desktop
articalIDStrings = getArticalIDStringFromLocalHtmlFile('/Users/mac/Desktop/test.htm')
for articalIDString in articalIDStrings:
likeArticle(articalIDString)