Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ def __init__(self, message):

def download_captcha():
url = "https://www.zhihu.com/captcha.gif"
r = requests.get(url, params={"r": random.random(), "type": "login"}, verify=False)
headers = {
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36",
'Host': "www.zhihu.com",
'Origin': "http://www.zhihu.com",
'Pragma': "no-cache",
'Referer': "http://www.zhihu.com/",
'X-Requested-With': "XMLHttpRequest"
}
r = requests.get(url, params={"r": random.random(), "type": "login"}, verify=False, headers=headers)
if int(r.status_code) != 200:
raise NetworkError(u"验证码请求失败")
image_name = u"verify." + r.headers['content-type'].split("/")[1]
Expand Down Expand Up @@ -96,7 +104,15 @@ def download_captcha():

def search_xsrf():
url = "http://www.zhihu.com/"
r = requests.get(url, verify=False)
headers = {
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36",
'Host': "www.zhihu.com",
'Origin': "http://www.zhihu.com",
'Pragma': "no-cache",
'Referer': "http://www.zhihu.com/",
'X-Requested-With': "XMLHttpRequest"
}
r = requests.get(url, verify=False, headers=headers)
if int(r.status_code) != 200:
raise NetworkError(u"验证码请求失败")
results = re.compile(r"\<input\stype=\"hidden\"\sname=\"_xsrf\"\svalue=\"(\S+)\"", re.DOTALL).findall(r.text)
Expand Down Expand Up @@ -163,7 +179,15 @@ def upload_form(form):
def islogin():
# check session
url = "https://www.zhihu.com/settings/profile"
r = requests.get(url, allow_redirects=False, verify=False)
headers = {
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36",
'Host': "www.zhihu.com",
'Origin': "http://www.zhihu.com",
'Pragma': "no-cache",
'Referer': "http://www.zhihu.com/",
'X-Requested-With': "XMLHttpRequest"
}
r = requests.get(url, allow_redirects=False, verify=False, headers=headers)
status_code = int(r.status_code)
if status_code == 301 or status_code == 302:
# 未登录
Expand Down
9 changes: 5 additions & 4 deletions zhihu.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def get_title(self):
if self.soup == None:
self.parser()
soup = self.soup
title = soup.find("h2", class_="zm-item-title").string.encode("utf-8").replace("\n", "")
title = soup.find("span", class_="zm-editable-content").string.encode("utf-8").replace("\n", "")
self.title = title
if platform.system() == 'Windows':
title = title.decode('utf-8').encode('gbk')
Expand Down Expand Up @@ -662,7 +662,7 @@ def get_topics_num(self):
if self.soup == None:
self.parser()
soup = self.soup
topics_num = soup.find_all("div", class_="zm-profile-side-section-title")[1].strong.string.encode("utf-8")
topics_num = soup.find_all("div", class_="zm-profile-side-section-title")[2].strong.string.encode("utf-8")
I=''
for i in topics_num:
if i.isdigit():
Expand Down Expand Up @@ -1067,7 +1067,7 @@ def get_question(self):
if self.soup == None:
self.parser()
soup = self.soup
question_link = soup.find("h2", class_="zm-item-title zm-editable-content").a
question_link = soup.find("h2", class_="zm-item-title").a
url = "http://www.zhihu.com" + question_link["href"]
title = question_link.string.encode("utf-8")
question = Question(url, title)
Expand Down Expand Up @@ -1144,7 +1144,8 @@ def to_txt(self):
if platform.system() == 'Windows':
anon_user_id = "匿名用户".decode('utf-8').encode('gbk')
else:
anon_user_id = "匿名用户"
anon_uget_titleser_id = "匿名用户"
anon_user_id = "匿名用户".decode('utf-8').encode('gbk')
if self.get_author().get_user_id() == anon_user_id:
if not os.path.isdir(os.path.join(os.path.join(os.getcwd(), "text"))):
os.makedirs(os.path.join(os.path.join(os.getcwd(), "text")))
Expand Down