python爬虫爬取雪球股票

from urllib import request
import json
base_url = "https://xueqiu.com/stock/cata/stocklist.json?page=1&size=30&order=desc&orderby=percent&type=11%2C12"
head={"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
      "Cookie": "device_id=ee962565c38fedf650595add24424b5a; _ga=GA1.2.1879860892.1543973630; _gid=GA1.2.1745833750.1543973630; s=dr122ds6ra; aliyungf_tc=AQAAACIZ+RIr9AMA7lc4OkP2sJbWVMt1; xq_a_token=6125633fe86dec75d9edcd37ac089d8aed148b9e; xq_a_token.sig=CKaeIxP0OqcHQf2b4XOfUg-gXv0; xq_r_token=335505f8d6608a9d9fa932c981d547ad9336e2b5; xq_r_token.sig=i9gZwKtoEEpsL9Ck0G7yUGU42LY; _gat_gtag_UA_16079156_4=1; Hm_lvt_1db88642e346389874251b5a1eded6e3=1543973640,1543974054; u=311543974054482; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1543974057; __utma=1.1879860892.1543973630.1543974057.1543974057.1; __utmc=1; __utmz=1.1543974057.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmt=1; __utmb=1.2.10.1543974057"}
req =request.Request(url=base_url,headers=head)
res = request.urlopen(req)
base_data=res.read().decode("utf-8")
new_data = json.loads(base_data)
print(new_data)
for i in new_data["stocks"]:
    # print(i)
    with open("./雪球.json","a+",encoding="utf-8") as f:
        f.write(json.dumps(i,ensure_ascii=False)+"\n")

你可能感兴趣的:(python)