python爬去内涵段子(json文件处理)

import requests
import time

url = "http://neihanshequ.com/joke/?is_json=1&app_name=neihanshequ_web&max_time=1521106836"
headers = {"Accept" :"application/json, text/javascript, */*; q=0.01",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.9",
"Cache-Control":"no-cache",
"Connection":"keep-alive",
"Cookie":'csrftoken=39d061a82943a131cbd937c4055726a9; tt_webid=6532073659451803139; uuid="w:d8f38aaaa8eb4c419b86571d6e8fd0a4"; _ga=GA1.2.884661281.1520866917; _gid=GA1.2.322191654.1521025498',
"Host":"neihanshequ.com",
"Pragma":"no-cache",
"Referer":"http://neihanshequ.com/",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36",
"X-CSRFToken":"39d061a82943a131cbd937c4055726a9",
"X-Requested-With":"XMLHttpRequest"
}
response = requests.get(url, headers=headers)
timestamp = response.json()["data"]["max_time"]
print(timestamp)
while type(timestamp)== float or type(timestamp)== int:
    time.sleep(3)
    response = requests.get(url, headers=headers)
    timestamp = response.json()["data"]["max_time"]
    with open('C:\\Users\\wwxy\\Desktop\\内涵段子\\data.txt','a',encoding='utf-8') as f:
        for i in range(len(response.json()["data"]["data"])):
            data = response.json()["data"]["data"][i]["group"]["content"]
            f.write(data + "\n")
            f.write("\n")
    url = "http://neihanshequ.com/joke/?is_json=1&app_name=neihanshequ_web&max_time=" + str(timestamp)
    print("已完成一次爬取",timestamp)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值