import requests
import pandas as pd
import time
# 获取每页数据
def get_r(page):
url = 'https://www.toutiao.com/article/v2/tab_comments/'
parames = {
'aid': 24,
'app_name': 'toutiao_web',
'offset': (page - 1) * 50,
'count': 50,
'group_id': 1689580945358862,
'item_id': 1689580945358862,
'_signature': '_02B4Z6wo00101IZvL-QAAIDDNp7DLQUxIpCGSitAAEGVC86zvKTfCRp-Wa5ANNYQfh2WgJumlc0EwRxHlBXpQfGU21441ffJ1RLN.qHCZ33.fOX8DklzsWQNwEzpSQU3jMrfczAcJUflkfJib6'
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36",
}
r = requests.get(url, params=parames, headers=headers)
return r
# 解析每页评价数据
def get_comment(r):
j = r.json()
data_list = j['data']
comments = []
for data in data_list:
content = data['comment']
comment = {}
comment['create_time'] = content['create_time']
comment['digg_count'] = content['digg_count']
comment['reply_count'] = content['reply_count']
comment['text'] = content['text']
comment['user_id'] = content['user_id']
comment['user_name'] = content['user_name']
comment['verified_reason'] = content['verified_reason']
comments.append(comment)
return comments
# 主函数
def main():
r = get_r(1)
j = r.json()
total_number = j['total_number']
pages = total_number // 50 + 1
print(f'一共 {total_number} 条评价,共 {pages} 页')
comments_list = []
for page in range(1, pages + 1):
# time.sleep(0.5)
r = get_r(page)
comments = get_comment(r)
comments_list.extend(comments)
print(f'第 {page} 页评价已爬取')
df = pd.DataFrame(comments_list)
df['create_time'] = df['create_time'].apply(lambda x: pd.Timestamp(x, unit="s", tz='Asia/Shanghai'))
df.drop_duplicates(subset='user_id', inplace=True)
return df
if __name__ == "__main__":
df = main()
df.to_csv("./张碧晨.csv")
爬虫今日头条--张碧晨
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Hello, 感觉好久没有写简书了,最近一直在忙支付和新需求,忙里偷闲学了一下 python 的简单使用,然后尝试...
- GitHub 今日头条搜索 F12 找到相应的js 用此链接 requests 获取相应的html 嫌长可以url...
- 之前我们爬取的网页,多是HTML静态生成的内容,直接从HTML源码中就能找到看到的数据和内容,然而并不是所有的网页...
- 今日头条的用户页数据爬取跟频道页的数据爬取大部分很类似,但稍微有一点不一样,就是用户主页的接口signature有...