爬虫爬取新浪财经纸浆行业期货年线数据

亲测有效,抓包页面在这里点进新浪财经网页,点击上方期货,左侧滑动,点击你想要抓取的行业,然后进入有图线的页面,点击年线,在这个页面又键检查即可,找到带有Dailykline的包。输入下面代码(换url,换headers)最终结果在最后。

爬虫爬取新浪财经纸浆行业期货年线数据_第1张图片

爬虫爬取新浪财经纸浆行业期货年线数据_第2张图片 

爬虫爬取新浪财经纸浆行业期货年线数据_第3张图片 

 

import csv
import requests
import re
import json
f = open('年线.csv',mode='a',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=['日期','开盘','最高','最低','收盘','成交量','均价'])
csv_writer.writeheader()
url = 'https://stock2.finance.sina.com.cn/futures/api/jsonp.php/var%20_SP02023_12_17=/InnerFuturesNewService.getDailyKLine?symbol=SP0&_=2023_12_17'
headers = {'User-Agent':
               'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
           }

response = requests.get(url=url,headers=headers)

content = re.findall('\{(.*?)\}',response.text)
for index in content:
    index_dict = '{'+index+'}'
    json_data = json.loads(index_dict)
    dit = {
        '日期':json_data['d'],
        '开盘': json_data['o'],
        '最高': json_data['h'],
        '最低': json_data['l'],
        '收盘': json_data['c'],
        '成交量': json_data['v'],
        '均价': json_data['s']

    }
    csv_writer.writerow(dit)

爬虫爬取新浪财经纸浆行业期货年线数据_第4张图片

 爬虫爬取新浪财经纸浆行业期货年线数据_第5张图片

 

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