其他
笑死人不偿命的知乎沙雕问题排行榜 「文末福利」
The following article is from 数据森麟 Author 数据森麟
好好学习(图片来自于网络)
作者:徐麟,某互联网公司数据分析狮,个人公众号数据森麟(id:shujusenlin)
前言
数据来源
import selenium
from selenium import webdriver
import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
driver = webdriver.Chrome()
driver.maximize_window()
url = 'https://www.zhihu.com/question/37453271'
js='window.open("'+url+'")'
driver.execute_script(js)
driver.close()
driver.switch_to_window(driver.window_handles[0])
for i in range(100):
js="var q=document.documentElement.scrollTop=10000000"
driver.execute_script(js)
all_html = [k.get_property('innerHTML') for k in driver.find_elements_by_class_name('AnswerItem')]
all_text = ''.join(all_html)
#all_text = all_text.replace('\u002F','/')
all_text = all_text.replace('questions','question')
pat = 'question/\d+'
questions = list(set([k for k in re.findall(pat,all_text)]))
'Connection': 'keep-alive'}
cookies ='v=3; iuuid=1A6E888B4A4B29B16FBA1299108DBE9CDCB327A9713C232B36E4DB4FF222CF03; webp=true; ci=1%2C%E5%8C%97%E4%BA%AC; __guid=26581345.3954606544145667000.1530879049181.8303; _lxsdk_cuid=1646f808301c8-0a4e19f5421593-5d4e211f-100200-1646f808302c8; _lxsdk=1A6E888B4A4B29B16FBA1299108DBE9CDCB327A9713C232B36E4DB4FF222CF03; monitor_count=1; _lxsdk_s=16472ee89ec-de2-f91-ed0%7C%7C5; __mta=189118996.1530879050545.1530936763555.1530937843742.18'
cookie = {}
for line in cookies.split(';'):
name, value = cookies.strip().split('=', 1)
cookie[name] = value
questions_df = pd.DataFrame(columns = ['title','visit','follower','answer','is_open'])
for i in range(len(questions)):
try:
url = 'https://www.zhihu.com/'+questions[i]
html = requests.get(url,cookies=cookie, headers=header).content
bsObj = BeautifulSoup(html.decode('utf-8'),"html.parser")
text = str(bsObj)
title = bsObj.find('h1',attrs={'class':'QuestionHeader-title'}).text
visit = int(re.findall('"visitCount":\d+',text)[0].replace('"visitCount":',''))
follower = int(re.findall('"followerCount":\d+',text)[0].replace('"followerCount":',''))
answer = int(re.findall('"answerCount":\d+',text)[0].replace('"answerCount":',''))
is_open = int(len(re.findall('问题已关闭',text))==0)
questions_df = questions_df.append({'title':title,'visit':visit,
'follower':follower,'answer':answer,
'is_open':is_open},ignore_index=True)
time.sleep(2)
print(i)
except:
print('错误'+str(i))
数据分析
沙雕问题排行榜
TOP 15
TOP 14
TOP 13
TOP 12
TOP 11
TOP 10
TOP 9
TOP 8
TOP 7
TOP 6
TOP 5
TOP 4
TOP 3
TOP 2
TOP 1
写在最后
【完】
说句题外话,有不少人想加我微信,看我朋友圈的每日分享,我姑且放出来,但名额有限,先来先得。我的朋友圈不止有技术分享,更有我的日常感悟,还有我个人商业思维观点 速速扫码添加!
扫码添加,备注:公号铁粉