Update zhihu.py

This commit is contained in:
guanjihuan 2023-09-02 16:19:17 +08:00
parent 822fa7e626
commit e6bca4a3a0

View File

@ -13,10 +13,21 @@ month = datetime.datetime.now().month
day = datetime.datetime.now().day 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)): for i0 in range(len(authors)):
start_link = authors[i0] start_link = authors[i0]
html = urlopen(start_link).read().decode('utf-8') # 打开网页 html = urlopen(start_link).read().decode('utf-8') # 打开网页
@ -29,6 +40,8 @@ for i0 in range(len(authors)):
href = 'https:'+ href href = 'https:'+ href
if href not in match_href: if href not in match_href:
match_href.append(href) match_href.append(href)
# 对链接进行排序 # 对链接进行排序
numbers = [] numbers = []
match_href_new = [] match_href_new = []
@ -38,9 +51,10 @@ numbers.sort(reverse = True)
for n in numbers: for n in numbers:
match_href_new.append('https://zhuanlan.zhihu.com/p/'+str(n)) match_href_new.append('https://zhuanlan.zhihu.com/p/'+str(n))
# 获取内容并写入文件 # 获取内容并写入文件
f = open('zhihu.html', 'w', encoding='UTF-8') f = open('zhihu.html', 'w', encoding='UTF-8')
f.write('<meta charset="utf-8"><style type="text/css">a{text-decoration: none;color: #0a5794;}a:hover {text-decoration: underline;color: red; }</style>') f.write('<meta charset="utf-8"><style type="text/css">a{text-decoration: none;color: #004e4e;}a:hover {text-decoration: underline;color: red; }</style>')
f.write('<p>'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新</p>') f.write('<p>'+str(year)+'.'+str(month).rjust(2,'0')+'.'+str(day).rjust(2,'0')+' 已更新</p>')
for href in match_href_new: for href in match_href_new:
try: try: