python爬网易新闻评论,直接上代码吧。。。
【完整代码】
from bs4 import BeautifulSoup
import requests
import json,datetime,os
today=str(datetime.datetime.now())[5:7]+str(datetime.datetime.now())[8:10]
headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/\
537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
news_num=0
mulu=''
print('本程序自动爬取网易新闻(国内/国际/要闻版)首页的70条新闻评论。生成文件并保存。\n\n制作:云云\n完工:2021-6-3')
key0=input('\n输入1-国内新闻,2-国际新闻,3-要闻:')
duiying={'1':'guonei','2':'guoji','3':'yaowen20200213'}
key=duiying[key0]
foldername=today+key+'/'
try:
os.makedirs(foldername)
except:
pass
def getcomments(idd):
comment_num=0
content=''
url1='https://comment.api.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/'
url2='/comments/hotList'
url=url1+idd+url2
params={'ibc': 'newspc',