-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathgetCollections.py
200 lines (172 loc) · 5.87 KB
/
getCollections.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*-coding=utf-8-*-
__author__ = 'Rocky'
import requests
import cookielib
import re
import json
import time
from bs4 import BeautifulSoup
import os
from lxml import etree
import codecs
session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename="cookies")
agent = 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'
headers = {'Host': 'www.zhihu.com',
'Referer': 'https://www.zhihu.com',
'User-Agent': agent}
try:
session.cookies.load(ignore_discard=True)
except:
print "Cookie can't load"
def getUserData(configFile):
f = open(configFile, 'r')
#for i in f.readlines():
data=f.readlines()
username=data[0].strip()
pwd=data[1].strip()
return username,pwd
def getCaptcha():
#r=1471341285051
r = (time.time() * 1000)
url = 'http://www.zhihu.com/captcha.gif?r=' + str(r) + '&type=login'
image = session.get(url, headers=headers)
f = open("photo.jpg", 'wb')
f.write(image.content)
f.close()
def get_xsrf():
url = 'https://www.zhihu.com'
r = session.get(url, headers=headers, allow_redirects=False)
txt = r.text
#print txt
result = re.findall(r'<input type=\"hidden\" name=\"_xsrf\" value=\"(\w+)\"/>', txt)[0]
return result
def Login():
username,pwd=getUserData("data.cfg")
xsrf = get_xsrf()
print xsrf
print len(xsrf)
login_url = 'https://www.zhihu.com/login/email'
data = {
'_xsrf': xsrf,
'password': pwd,
'remember_me': 'true',
'email': username
}
try:
content = session.post(login_url, data=data, headers=headers)
login_code = content.text
d = json.loads(login_code)
#print d['msg']
#print content.status_code
#this line important ! if no status, if will fail and execute the except part
#print content.status
if content.status_code != requests.codes.ok:
print "Need to verification code !"
getCaptcha()
#print "Please input the code of the captcha"
code = raw_input("Please input the code of the captcha")
data['captcha'] = code
content = session.post(login_url, data=data, headers=headers)
print content.status_code
if content.status_code == requests.codes.ok:
print "Login successful"
session.cookies.save()
#print login_code
else:
session.cookies.save()
return True
except:
print "Error in login"
return False
def isLogin():
url = 'https://www.zhihu.com/settings/profile'
login_code = session.get(url, headers=headers, allow_redirects=False).status_code
print login_code
if login_code == 200:
return True
else:
return False
def save2file(filename, content):
# 保存为电子书文件
filename = filename + ".txt"
f = codecs.open(filename, 'a',encoding='utf-8')
f.write(content)
f.close()
def getAnswer(url):
#这个功能已经实现
html=session.get(url,headers=headers,allow_redirects=False)
s=html.text
tree=etree.HTML(s)
title=tree.xpath('//title/text()')[0]
filename_old = title.strip()
filename = re.sub('[\/:*?"<>|]', '-', filename_old)
# 用来保存内容的文件名,因为文件名不能有一些特殊符号,所以使用正则表达式过滤掉
print filename
save2file(filename, title)
save2file(filename, "\n\n--------------------Link %s ----------------------\n" %url)
save2file(filename, "\n\n--------------------Detail----------------------\n\n")
# 获取问题的补充内容
content=tree.xpath('//div[@class="zm-editable-content clearfix"]')
for i in content:
#print i
text_content=i.xpath("string(.)")
save2file(filename,text_content)
print "Done"
def getCollections():
#实现,获取所有的collection 的link
links=[]
url='https://www.zhihu.com/collections/mine'
Login()
if isLogin():
content=session.get(url,headers=headers,allow_redirects=False)
s= content.text
p=re.compile(r'<h2 class=\"zm-item-title\">\s+<a href=\"(.*?)\" >')
result=p.findall(s,re.S)
if result is not None:
return result
else:
return None
def getEachQuestion(url):
s=session.get(url,headers=headers,allow_redirects=False)
tree=etree.HTML(s.text)
result=tree.xpath('//link[@itemprop="url"]/@href')
return result
if __name__=='__main__':
sub_folder = os.path.join(os.getcwd(), "collections")
# 专门用于存放下载的电子书的目录
if not os.path.exists(sub_folder):
os.mkdir(sub_folder)
os.chdir(sub_folder)
host='https://www.zhihu.com'
collection_link=getCollections()
for i in collection_link:
print i
page=1
while 1:
scan_link=collection_url=host+i+'?page=%d' %page
return_content=session.get(scan_link,headers=headers,allow_redirects=False).text
tree=etree.HTML(return_content)
result=tree.xpath('//link[@itemprop="url"]/@href')
for j in result:
print j
pttrn=re.compile('zhuanlan')
if pttrn.findall(j):
print j
print "skip zhuanlan first"
continue
getAnswer(host+j)
p=re.compile(u'<span class="zg-gray-normal">下一页</span>')
if p.search(return_content):
break
p2=re.compile(u'下一页')
if p2.search(return_content) is None:
break
page=page+1
'''
collection=['https://www.zhihu.com'+i for i in collection_link]
print collection
'''
#url='https://www.zhihu.com/collection/40627095'
#getEachQuestion(url)
#getAnswer('https://www.zhihu.com/question/30348020/answer/144386645')