diff --git a/language_learning/python/2021.11.17_zhihu/zhihu.py b/language_learning/python/2021.11.17_zhihu/zhihu.py index e2b1b9b..39a8d4a 100644 --- a/language_learning/python/2021.11.17_zhihu/zhihu.py +++ b/language_learning/python/2021.11.17_zhihu/zhihu.py @@ -13,10 +13,21 @@ month = datetime.datetime.now().month day = datetime.datetime.now().day # 获取链接 -match_href = [] + # 由于没有模拟登录知乎,因此只能爬取到最新的两篇文章 -authors = ["https://www.zhihu.com/people/guanjihuan/posts", # Guan -] +authors = ["https://www.zhihu.com/people/guanjihuan/posts"] # Guan + +# authors = [] +# html = urlopen('https://www.guanjihuan.com/others').read().decode('utf-8') # 打开网页 +# soup = BeautifulSoup(html, features='lxml') # 放入soup中 +# all_a_tag = soup.find_all('a', href=True) # 获取超链接标签 +# for a_tag in all_a_tag: +# href = a_tag['href'] # 超链接字符串 +# if re.search('www.zhihu.com/people/', href): # 文章的链接 +# if href not in authors: +# authors.append(href) + +match_href = [] for i0 in range(len(authors)): start_link = authors[i0] html = urlopen(start_link).read().decode('utf-8') # 打开网页 @@ -29,6 +40,8 @@ for i0 in range(len(authors)): href = 'https:'+ href if href not in match_href: match_href.append(href) + + # 对链接进行排序 numbers = [] match_href_new = [] @@ -38,9 +51,10 @@ numbers.sort(reverse = True) for n in numbers: match_href_new.append('https://zhuanlan.zhihu.com/p/'+str(n)) + # 获取内容并写入文件 f = open('zhihu.html', 'w', encoding='UTF-8') -f.write('') +f.write('') f.write('

'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新

') for href in match_href_new: try: