# - * - coding: utf-8 - * -
# author: Albert time: 2019/9/3
The import requests, json, time, CSV
The from fake_useragent import UserAgent # for UserAgent
The from datetime import datetime, timedelta
?
Def get_content (url) :
"' API information web site source code" '
Ua=UserAgent (). The random
Try:
Data=https://bbs.csdn.net/topics/requests.get (url, headers={' the user-agent: ua}, the timeout=3). The text
The return data
Except:
Pass
Def Process_data (HTML) :
"' the contents of the data acquisition '
Data_set_list=[]
# json formatted
Data_list=json. Loads (HTML) [' CMTS ']
For the data in data_list:
Data_set=[data [' id '], data [' nickName '], data [' userLevel], data [' cityName '], data [' content '], data [' score '], data [' startTime ']]
Data_set_list. Append (data_set)
Return data_set_list
?
If __name__=="__main__ ':
Start_time=start_time=datetime. Now (). Strftime (' % % Y - m - H: % d % % m: % S ') # to get the current time, from the current time forward for
# print (start_time)
End_time='2019-07-26 08:00:00'
?
# print (end_time)
While start_time & gt; STR (end_time) :
# construct url
Url="http://m.maoyan.com/mmdb/comments/movie/1211270.json? _v_=yes& Offset=0 & amp; StartTime='+ start_time. Replace (
"', '% 20')
Print ('... ')
Try:
HTML=get_content (url)
Except the Exception as e:
Time. Sleep (0.5)
HTML=get_content (url)
The else:
Time. Sleep (1)
Comments=Process_data (HTML)
# print (comments [14] [1])
If comments:
Start_time=comments [14] [1]
Start_time=datetime. Strptime (start_time, '% % Y - m - H: % d % % m: % S') + timedelta (seconds=1)
# print (start_time)
Start_time=datetime. Strftime (start_time, '% % Y - m - H: % d % % m: % S')
Print (comments)
# save data for CSV
With the open (" comments_1. CSV ", "a", encoding="utf-8", newline=' ') as csvfile:
Writer.=the CSV writer (csvfile)
Writer. Writerows (comments)
?
Run the above code is read on the Internet but will have the following case, who is a great god can give me a hand will make the final assignment in a
CodePudding user response:
Now data is out there are bosses know how to use the data for related visualization operation? (such as pie charts, region distribution, etc.)