作者计划 2021 fall 出国留学,偶然在知乎上看到这个问题(原问题网页):
2021 fall你都申请了哪些学校的MS/PhD?录取结果如何?
我出于好奇,爬取了这个问题下所有的回答,以便各位分析。
我本人打算申请统计类相关的专业,于是写了一个简单的关键词搜索。找出所有回答里和统计沾边的回答,共197个(真卷,要失学)
读者可以自行进行关键词搜索,或者自己写函数,分析每位答主的三维,本科院校,目标院校,录取结果等等。
具体代码如下:
爬取部分:
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 31 14:41:46 2020
@author: zxw
"""
# 引入必要的库
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json
import time
import re
def get_driver():
try:
return webdriver.PhantomJS()
except Exception:
return webdriver.Firefox()
# 得到登录的cookie
def login_cookie():
driver = get_driver()
driver.set_page_load_timeout(20)
driver.set_script_timeout(20)
LOGIN_URL = 'https://www.zhihu.com/'
driver.get(LOGIN_URL)
time.sleep(5)
input("请登录后按 Enter")
cookies = driver.get_cookies()
jsonCookies = json.dumps(cookies)
#下面的文件位置需要自己改
with open('C:/Users/zxw/Desktop/修身/与自己/数据科学/爬虫/cookies/zhihu.txt','w') as f:
f.write(jsonCookies)
driver.quit()
# 再次登录
def login():
driver.set_page_load_timeout(20)
driver.set_script_timeout(20)
LOGIN_URL = 'https://www.zhihu.com/'
driver.get(LOGIN_URL)
time.sleep(5)
#下面的文件位置需要自己改,与上面的改动一致
f = open('C:/Users/zxw/Desktop/修身/与自己/数据科学/爬虫/cookies/zhihu.txt')
cookies = f.read()
jsonCookies = json.loads(cookies)
for co in jsonCookies:
driver.add_cookie(co)
driver.refresh()
time.sleep(5)
# 爬取某问题下的所有答案
def get_answers(question_url):
driver.get(question_url)
number_text = driver.find_element_by_partial_link_text('查看全部').text
number = int(re.search('[0-9]+',number_text).group())
driver.find_element_by_partial_link_text('查看全部').click()
for k in range(number):
xpath = '/html/body/div[1]/div/main/div/div[2]/div[1]/div/div[2]/div/div/div/div[2]/div/div[{}]/div/div[2]/div[1]/span'.format(k+1)
element = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, xpath)))
answer = element.text
#下面的文件位置需要自己改,保存到你想要的位置
file = open('C:/Users/zxw/Desktop/修身/与自己/数据科学/爬虫/2021 fall stat/answer{}.txt'.format(k+1),'w',encoding='utf-8')
file.write(answer)
file.close()
print('answer '+ str(k+1) +' collected!')
time.sleep(1)
js="window.scrollTo(0,document.body.scrollHeight)"
driver.execute_script(js)
time.sleep(1)
if __name__ == "__main__":
# 设置你想要搜索的问题
question_url = 'https://www.zhihu.com/question/357928233/answer/1682053705'
login_cookie()
driver = get_driver()
login()
get_answers(question_url)
关键词搜索:
import os
import re
# 修改文件路径至存储数据的目录
os.chdir('C:/Users/zxw/Desktop/修身/与自己/数据科学/爬虫/2021 fall stat')
# 截至2021年1月18日13时50分的回答数
num = 811
# 关键词搜索
def key_word_search(key_word):
# 目标回答
target_list = []
for k in range(num):
with open('answer{}.txt'.format(k+1),'r',encoding='utf-8') as f:
text = f.read()
Flag = 1
for word in key_word:
if re.search(word, text,re.IGNORECASE) == None:
pass
else:
Flag = 0
if Flag == 0:
target_list.append(text)
return target_list
# 设置关键词
key_word = ['stat','统计','ds','data science','数据科学']
target_list = key_word_search(key_word)