-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path_知乎__问答_文章.py
180 lines (152 loc) · 7.29 KB
/
_知乎__问答_文章.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
import json
import os
import time
import traceback
import pyautogui
from selenium import webdriver # 导入selenium包
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from settings import zhihu_cookie_path, zhihu_answer_path, zhihu_article_path, record_url_local
from tools import getResModel, excel, download_pic, log
class Zhihu:
def __init__(self):
if not os.path.exists(zhihu_answer_path):
os.mkdir(zhihu_answer_path)
if not os.path.exists(zhihu_article_path):
os.mkdir(zhihu_article_path)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
chrome_options.add_argument("--disable-blink-features")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
s = Service('./chromedriver.exe')
# s = Service("Driver/geckodriver.exe")
self.driver = webdriver.Chrome(service=s, options=chrome_options)
self.__updateCookie()
def setKeyword(self, keywords:list):
self.keyword = " ".join(keywords)
def __updateCookie(self):
self.driver.get('https://www.zhihu.com/search?type=content&q=')
time.sleep(5)
if not os.path.exists(zhihu_cookie_path):
time.sleep(40) # 留够时间来手动登录
with open(zhihu_cookie_path, 'w') as f:
# 将cookies保存为json格式在cookies.txt中
f.write(json.dumps(self.driver.get_cookies()))
else:
self.driver.delete_all_cookies()
with open(zhihu_cookie_path, 'r') as f:
cookies_list = json.load(f) # 读取cookies
for cookie in cookies_list:
if isinstance(cookie.get('expiry'), float):
cookie['expiry'] = int(cookie['expiry'])
try:
self.driver.add_cookie(cookie) # 加入cookies
except:
print(cookie)
self.driver.refresh()
def __scrollToBottom(self, scrollTime:int):
for i in range(scrollTime):
pos = pyautogui.size()
pyautogui.moveTo(pos.width / 2, pos.height / 2)
pyautogui.scroll(-400)
time.sleep(0.2)
def searchArticle(self, scrollTime:int):
self.driver.get('https://www.zhihu.com/search?q={}&type=content&utm_id=0&vertical=article'.format(self.keyword))
self.driver.maximize_window()
time.sleep(2)
self.__scrollToBottom(scrollTime)
# number = 0
urls = []
introductions = []
for article in self.driver.find_elements(By.CLASS_NAME, "ContentItem.ArticleItem"):
title = article.find_element(By.CSS_SELECTOR, '[target="_blank"]')
url = title.get_attribute('href')
urls.append(url)
introduction = article.find_element(By.CSS_SELECTOR, '[itemprop="articleBody"]').text
introductions.append(introduction)
print(url)
# number+=1
# print(number)
# time.sleep(600)
for i in range(len(urls)):
self.__openArticle(urls[i], introductions[i])
# time.sleep(600)
# return
time.sleep(2)
# driver.find_element(By.ID, "Popover1-toggle").send_keys("玫瑰\n")
return
def searchAnswer(self, scrollTime:int):
self.driver.get('https://www.zhihu.com/search?q={}&type=content&utm_id=0&vertical=answer'.format(self.keyword))
self.driver.maximize_window()
time.sleep(2)
self.__scrollToBottom(scrollTime)
# number = 0
urls = []
for answer in self.driver.find_elements(By.CLASS_NAME, "ContentItem.AnswerItem"):
title = answer.find_element(By.CSS_SELECTOR, '[itemprop="url"]')
url = title.get_attribute('content')
urls.append(url)
print(url)
# number+=1
# print(number)
for url in urls:
try:
self.__openQuestion(url, scrollTime)
except:
log(url)
log(traceback.format_exc())
# return
# time.sleep(600)
time.sleep(2)
# driver.find_element(By.ID, "Popover1-toggle").send_keys("玫瑰\n")
return
def __openArticle(self, url:str, introduction:str):
if excel.checkHttpRepeat(url):
return
self.driver.get(url)
time.sleep(2)
title = self.driver.find_element(By.CLASS_NAME, "Post-Title").text
# introduction = "-"
author = self.driver.find_element(By.CLASS_NAME, "UserLink.AuthorInfo-name").find_element(By.CLASS_NAME, 'UserLink-link').text
pics_attrs = self.driver.find_element(By.CLASS_NAME, "Post-RichTextContainer").find_elements(By.TAG_NAME, "img")
for pic_attr in pics_attrs:
pic_url = pic_attr.get_attribute("data-original")
if pic_url is None:
continue
temp = self.__divideZhihu(source="知乎文章", author=author, title=title,
introduction=introduction,
url_article=url, url_pic=pic_url,
url_local=zhihu_article_path + self.keyword + str(excel.nrows) + ".png")
print(temp)
download_pic(pic_url, temp[record_url_local])
excel.appendRecord(temp)
def __openQuestion(self, url:str, scrollTime:int):
if excel.checkHttpRepeat(url):
return
self.driver.get(url)
time.sleep(2)
self.__scrollToBottom(scrollTime)
title = self.driver.find_element(By.CLASS_NAME, "QuestionHeader-title").text
introduction = self.driver.find_element(By.CLASS_NAME, "RichText.ztext.css-1g0fqss").text
answers = self.driver.find_elements(By.CLASS_NAME, "ContentItem.AnswerItem")
for answer in answers:
author = answer.find_element(By.CLASS_NAME, "AuthorInfo").find_element(By.CSS_SELECTOR, '[itemprop="name"]').get_attribute("content")
pics_attrs = answer.find_element(By.CLASS_NAME, "RichContent-inner").find_elements(By.TAG_NAME, "img")
for pic_attr in pics_attrs:
pic_url = pic_attr.get_attribute("data-original")
if pic_url is None:
continue
temp = self.__divideZhihu(source="知乎问答", author=author, title=title,
introduction=introduction,
url_article=url, url_pic=pic_url,
url_local=zhihu_answer_path + self.keyword + str(excel.nrows) + ".png")
print(temp)
download_pic(pic_url, temp[record_url_local])
excel.appendRecord(temp)
def __divideZhihu(self, source:str, author: str, title:str, introduction:str,
url_article: str, url_pic: str, url_local:str):
res = getResModel(type='图片', source=source, keyword=self.keyword,
author=author, title=title, introduction=introduction,
tags=["-"], caption="-", url_article=url_article,
url_pic=url_pic, url_local=url_local)
return res