用python+selenium抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答并保存至html文件

抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答,保存至html文件,该html文件的文件名应该是20160228_zhihu_today_hot.html,也就是日期+zhihu_today_hot.html

代码如下:

 from selenium import webdriver
 from time import sleep
 import time

 class ZhiHu():

     def __init__(self):
         self.dr = webdriver.Chrome()
         self.dr.maximize_window()
         self.today_hot_list = self.get_today_hot()
         self.month_hot_list = self.get_month_hot()

     def get_today_hot(self):
         '''知乎今日最热问题前3个'''
         today_hot = []
         i = 0
         while i < 3:
             self.dr.get('https://www.zhihu.com/explore')
             sleep(3)
             question_title = self.dr.find_elements_by_css_selector('div.explore-feed.feed-item>h2>a.question_link')[i].text #获取问题
             question_answer_url = self.dr.find_elements_by_css_selector('div.explore-feed.feed-item>h2>a.question_link')[i].get_attribute('href') #获取问题回答的url
             self.dr.get(question_answer_url) #访问问题url
             sleep(5)
             question_answer_innerhtml = self.dr.find_element_by_css_selector('.zm-editable-content.clearfix').get_attribute('innerHTML') #获取首个回答的innerHTML
             today_hot.append((question_title, question_answer_innerhtml))
             i += 1
         return today_hot

     def write_today_data(self):
         file_date = time.strftime('%Y-%m-%d',time.localtime(time.time()))
         self.file = open(file_date+'_zhihu_today_hot'+'.html','wb')
         file_line = '**********************************************<br />' #<br \>为转行符
         for item in self.today_hot_list:
             self.file.write(file_line.encode('gbk'))
             self.file.write(('问题:'+item[0]+'<br />').encode('gbk'))
             self.file.write(('首个回答:'+item[1]+'<br />').encode('gbk'))
         self.file.close()

     def get_month_hot(self):
         '''知乎本月最热问题前3个'''
         month_hot = []
         i = 5  # 本月最热前已有5个含explore-feed feed-item的div标签,所以这里含explore-feed.feed-item的div标签及其定义的i从5开始
         while i < 8:
             self.dr.get('https://www.zhihu.com/explore#monthly-hot')
             sleep(3)
             question_title = self.dr.find_elements_by_css_selector('div.explore-feed.feed-item>h2>a.question_link')[i].text  # 获取问题
             question_answer_url = self.dr.find_elements_by_css_selector('div.explore-feed.feed-item>h2>a.question_link')[i].get_attribute('href')  # 获取问题回答的url
             self.dr.get(question_answer_url)  # 访问问题url
             sleep(5)
             question_answer_innerhtml = self.dr.find_element_by_css_selector('.zm-editable-content.clearfix').get_attribute('innerHTML')  # 获取首个回答的innerHTML
             month_hot.append((question_title, question_answer_innerhtml))
             i += 1
         return month_hot

     def write_month_data(self):
         file_date = time.strftime('%Y-%m-%d', time.localtime(time.time()))
         self.file = open(file_date + '_zhihu_mouth_hot' + '.html', 'wb')
         file_line = '--------------------------------------<br />'
         for item in self.month_hot_list:
             self.file.write(file_line.encode('gbk'))
             self.file.write(('问题:' + item[0] + '<br />').encode('gbk'))
             self.file.write(('首个回答:' + item[1] + '<br />').encode('gbk'))
         self.file.close()

     def quit(self):
         self.dr.quit()

 if __name__ == '__main__':
     zhihu = ZhiHu()
     zhihu.write_today_data()
     zhihu.write_month_data()
     zhihu.quit()

网页如下:

用python+selenium抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答并保存至html文件

用python+selenium抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答并保存至html文件

生成html如下:

用python+selenium抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答并保存至html文件

用python+selenium抓取知乎今日最热和本月最热的前三个问题及每个问题的首个回答并保存至html文件

嘻嘻,html的排版不是多好哈~

上一篇:spring boot 使用redis进行发布订阅


下一篇:python模拟Get请求保存网易歌曲的url