用Python获取今日头条中关于游戏的相关信息1,代码修改可以随便查找其中的另外分类内容,比如科技,娱乐,体育之类的
import requests
class TouTiao():
def __init__(self):
"""
Initialize
"""
self.url = "https://www.toutiao.com/api/pc/feed/?category=news_game&utm_source=toutiao&widen=1&max_behot_time=0&max_behot_time_tmp=0&tadrequire=true&as=A1654D0AD58C2E6&cp=5DA5FCB27EC67E1&_signature=YkL0GwAAP-qKmNZmAa1p4mJC9A"
self.header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36",
'x-requested-with': 'XMLHttpRequest',
}
def get_html(self, max_behot_time):
"""
Get the website html sources
"""
try:
response = requests.get(self.url.format(
max_behot_time), headers=self.header)
except Exception as e:
print(e)
else:
return response.json()
def analyse(self, json):
"""
analyse the html file and get what we need
"""
data = json.get("data")
for item in data:
try:
content = {
"title": item.get("title").replace("\n", " "),
"source": item.get("source"),
"abstract": item.get("abstract"),
"comments_count": item.get("comments_count"),
"group_source": item.get("group_source"),
"source_url": "https://www.toutiao.com{}".format(item.get("source_url")),
}
except Exception:
continue
else:
for key, item in content.items():
print(key, ": ", item)
print("")
next_one = json.get("next")["max_behot_time"]
return next_one
def run(self):
"""
This function aims at spidering every possible web
"""
num = 1
print("--------------The {} part--------------".format(num))
json = self.get_html(1571143858) # 初始的,可自行查找
next = self.analyse(json)
print("")
while next:
print("--------------The {} part--------------".format(num))
num += 1
json = self.get_html(next)
next = self.analyse(json)
print("")
if __name__ == '__main__':
test = TouTiao()
test.run()
不想写作业,于是有此 ↩︎