mirror of
https://github.com/d0zingcat/RSSHub-python.git
synced 2026-06-12 07:26:49 +00:00
添加知乎 (www.zhihu.com) 的两个频道: 圆桌和收藏夹
This commit is contained in:
@@ -40,6 +40,10 @@ class ZhihuAnswer(AtomEntry):
|
||||
self.content = zhihu_figure_transfer(tree.css('.RichText').get())
|
||||
self.description = self.content
|
||||
|
||||
# author
|
||||
|
||||
self.author = json.loads(tree.xpath('//div[@class="ContentItem AnswerItem"]/@data-zop').get())['authorName']
|
||||
|
||||
meta: dict = get_value(json.loads(tree.css("#js-initialData::text").get())
|
||||
['initialState']['entities']['questions'])
|
||||
|
||||
@@ -51,9 +55,18 @@ class ZhihuZhuanlanArticle(AtomEntry):
|
||||
def get(self):
|
||||
tree = fetch(self.link)
|
||||
self.title = tree.css('h1::text').get()
|
||||
author = tree.xpath('//meta[@itemProp="name"]/@content').get()
|
||||
if author:
|
||||
self.author = author
|
||||
self.content = zhihu_figure_transfer(tree.css('article').css('.RichText').get())
|
||||
self.description = self.content
|
||||
|
||||
#
|
||||
data = json.loads(tree.css("#js-initialData::text").get())
|
||||
metadata = list(data['initialState']['entities']['articles'].values())[0]
|
||||
self.pubDate = datetime.fromtimestamp(metadata['created'])
|
||||
self.updated_time = datetime.fromtimestamp(metadata['updated'])
|
||||
|
||||
|
||||
class ZhihuQuestion(Feed):
|
||||
|
||||
|
||||
44
rsshub/spiders/zhihu/collection.py
Normal file
44
rsshub/spiders/zhihu/collection.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
from .article import ZhihuAnswer, ZhihuZhuanlanArticle
|
||||
|
||||
|
||||
|
||||
def get_metadata(collection_id):
|
||||
response = requests.get(f'https://api.zhihu.com/collections/{collection_id}')
|
||||
response.raise_for_status()
|
||||
data = json.loads(response.text)['collection']
|
||||
|
||||
metadata = dict()
|
||||
metadata['link'] = data['url']
|
||||
metadata['title'] = data['title']
|
||||
# metadata['created_time'] = data['created_time']
|
||||
# metadata['updated_time'] = data['updated_time']
|
||||
return metadata
|
||||
|
||||
def ctx(collection_id):
|
||||
|
||||
# meta
|
||||
metadata = get_metadata(collection_id)
|
||||
|
||||
# content
|
||||
|
||||
response = requests.get(f'https://www.zhihu.com/api/v4/collections/{collection_id}/items?limit=20&offset=0')
|
||||
response.raise_for_status()
|
||||
data = json.loads(response.text)
|
||||
items = []
|
||||
|
||||
for d in data['data']:
|
||||
if d['content']['type'] == 'answer':
|
||||
item = ZhihuAnswer(d['content']['url'])
|
||||
elif d['content']['type'] == 'article':
|
||||
item = ZhihuZhuanlanArticle(d['content']['url'])
|
||||
else:
|
||||
assert False
|
||||
item.get()
|
||||
items.append(item)
|
||||
|
||||
metadata['items'] = items
|
||||
|
||||
return metadata
|
||||
@@ -14,7 +14,7 @@ def ctx():
|
||||
discussion = tree.css('.ExploreRoundtableCard-questionTitle')
|
||||
collection_card = tree.css('.ExploreCollectionCard-contentTitle')
|
||||
|
||||
for post in chain(hot_question, collection_card, discussion, newest_topic):
|
||||
for post in chain(hot_question, collection_card, discussion): #, newest_topic):
|
||||
title = post.css('a::text').extract_first()
|
||||
link: str = post.css('a::attr(href)').extract_first()
|
||||
|
||||
|
||||
23
rsshub/spiders/zhihu/roundtable.py
Normal file
23
rsshub/spiders/zhihu/roundtable.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
from .article import ZhihuQuestion
|
||||
|
||||
def ctx(name):
|
||||
url = f'https://www.zhihu.com/api/v4/roundtables/{name}/hot-questions?include=data[*].question.relationship'
|
||||
response = requests.get(url)
|
||||
response.raise_for_status()
|
||||
|
||||
data = json.loads(response.text)
|
||||
items = []
|
||||
|
||||
for d in data['data']:
|
||||
item = ZhihuQuestion(f'https://www.zhihu.com/question/{d["question"]["id"]}')
|
||||
item.get_description()
|
||||
|
||||
items.append(item)
|
||||
|
||||
return {
|
||||
'title': 'roundtable',
|
||||
'items': items
|
||||
}
|
||||
Reference in New Issue
Block a user