Python3.x:访问带参数链接并且获取返回json串详解编程语言

Python3.x:访问带参数链接并且获取返回json串

示例一:

import json 
import xml.dom.minidom 
from urllib import request, parse 
from sqlalchemy import func 
 
url = "http://**********/queryTradingByProdTypeData.do" 
headerDict = {'Host': 'query.sse.com.cn', 
              'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0',  
              'Accept': '*/*', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',  
              'Accept-Encoding': 'gzip, deflate',  
              'Referer': 'http://**************/funddata/overview/day/',  
              'Connection': 'keep-alive'} 
jsonName = 'jsonpCallback24028' 
data = {'jsonCallBack': jsonName, 'searchDate': dataDate, 'prodType': 'jj'} 
params = parse.urlencode(data).encode('utf-8') 
req = request.Request(url, headers=headerDict) 
page = str(request.urlopen(req, data=params).read(), encoding="utf-8") 
 
rdata = page[len(jsonName) + 1:len(page) - 1] 
jsondata = json.loads(rdata) 
print("json>>:%s" %jsondata)

 示例二:

# 获取主要指标概览表 
def getChannelIdStr(channelFidStr): 
    url = "http://www.*****/getSubChannel.action" 
    headerDict = {'Host': 'www.*****.cn', 
                  'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.31 Safari/537.36', 
                  'Accept': 'application/json, text/javascript, */*; q=0.01', 
                  'Accept-Language': 'zh-CN,zh;q=0.8', 
                  'Accept-Encoding': 'gzip, deflate', 
                  'Referer': 'http://www.******/monthview.action?action=china', 
                  'Connection': 'keep-alive'} 
    data = {'channelFidStr': channelFidStr} 
    # psot 传递参数 
    res = requests.post(url, data=data, headers=headerDict) 
    # 获取跳转后的页面源码,返回json串 
    soup = BeautifulSoup(res.content, "html.parser") 
    print(soup) 
    # 转换json格式 
    jsonArr = json.loads(soup.text) 
    # 遍历json数组 
    channelId = [] 
    for jsonStr in jsonArr: 
        channelId.append(jsonStr['channelId']) 
        print(jsonStr['channelName']) 
    return channelId

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16777.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论