1

我正在尝试通过一个 tor 会话加载 JSON 数据。URL_1 工作正常,但 URL_2 不起作用(示例 I)。我不知道为什么..这是我得到的错误类型:

  • IP:<响应 [403]>

我可以在没有 tor 会话的情况下很好地恢复 Json api 数据(示例 II),但是一旦我为 URL_2 引入 requests.Session() 函数,它就不再起作用了。

例一

# open Tor browser before
import requests
from stem.control import Controller
from stem import Signal
import json

def get_tor_session():
    # initialize a requests Session
    session = requests.Session()
    # setting the proxy of both http & https to the localhost:9050 
    # this requires a running Tor service in your machine and listening on port 9050 (by default)
    session.proxies = {"http": "socks5://localhost:9150", "https": "socks5://localhost:9150"}
    return session

#url_1 = "https://api.cryptowat.ch/markets/kraken/eurusd/ohlc"
url_2 = 'https://api.1inch.exchange/v1.1/quote?fromTokenSymbol=USDT&toTokenSymbol=KAI&amount=1000000'

s = get_tor_session()
ip = s.get(url_2).text
#ipJ = json.loads(ip)
print(ip)

例二

import json
url_1 = 'https://api.cryptowat.ch/markets/kraken/eurusd/ohlc'
url_2 = 'https://api.1inch.exchange/v1.1/quote?fromTokenSymbol=USDT&toTokenSymbol=KAI&amount=1000000'
rA = requests.get(url_2) 
jsA = json.loads(rA.content) 
print(jsA)

我已经尝试在请求中添加标头,但效果不佳。

谢谢您的帮助

4

0 回答 0