爬虫作业

import requests
import json
import os
from threading import Timer


url='https://edu.cnblogs.com/Homework/GetAnswers?homeworkId=2420&_=1542959851766'

 
try:
    r=requests.get(url)
except:
    print("没有找到相应的页面")
   
r.encoding='utf-8'
shuju=json.loads(r.text)['data'] 
lingshi=[]

def getData():
    for data in shuju:
        lingshi.append(str(data['StudentNo']).replace('None','助教')+','+data['RealName']+','+data['Title']+','+data['DateAdded'].replace('T',' ')+','+data['Url']+'\n')
    
def getFile():
    file='hwlist.csv'
    with open (file,'w') as f:
        for i in lingshi:
            f.write(i)
       
getData()
getFile()










你可能感兴趣的:(爬虫作业)