Python Instagram页面无法通过带有Tor的urlopen()加载。原因/解决方法是什么?

Python Instagram页面无法通过带有Tor的urlopen()加载。原因/解决方法是什么?,python,python-3.x,python-requests,urllib,tor,Python,Python 3.x,Python Requests,Urllib,Tor,我正在通过urlopen()检查Instagram页面是否存在https://www.instagram.com/profile-name)。当配置文件页面存在时获取该页面,如果不存在,则返回404错误。这是一个完美的流程 但是Instagram请求限制很快就达到了。它是每个ip,所以我需要更改ip。为了这个,我已经试过了。而且。。。当我开始通过Tor connection执行urlopen()操作时,它会被破坏——获取Instagram登录页面时,不考虑配置文件的存在,因此我无法区分现有/不存

我正在通过
urlopen()检查Instagram页面是否存在https://www.instagram.com/profile-name)
。当配置文件页面存在时获取该页面,如果不存在,则返回404错误。这是一个完美的流程

但是Instagram请求限制很快就达到了。它是每个ip,所以我需要更改ip。为了这个,我已经试过了。而且。。。当我开始通过Tor connection执行
urlopen()
操作时,它会被破坏——获取Instagram登录页面时,不考虑配置文件的存在,因此我无法区分现有/不存在的配置文件。这种行为的原因可能是什么?如何解决

下面是示例代码。在
python3
中运行<代码>使用TOR常量将打开/关闭TOR。要安装
socks
在终端
pip3安装请求[socks]
pip3安装pysocks
中运行

在使用Tor之前,您需要先安装它

import urllib.request
from urllib.error import HTTPError
import socks
import socket

USE_TOR = True

def createConnection(address, timeout = None, source_address = None):
    sock = socks.socksocket()
    sock.connect(address)
    return sock

def getIp():
    with urllib.request.urlopen("http://httpbin.org/ip") as page:
        return str(page.read()).replace('\n', '')

#

print("Normal IP: " + getIp())

# Set up tor

if USE_TOR:
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
    socket.socket = socks.socksocket
    socket.create_connection = createConnection
    print("Tor IP: " + getIp())

# Request page

try:
    page = urllib.request.urlopen('https://www.instagram.com/a')
    print("Profile exists")
except HTTPError as e:
    print("Profile does not exist. Http error " + str(e.code))
终端输出:

USE\u TOR=True

Normal IP: b'{\n  "origin": "my ip"\n}\n'
Tor IP: b'{\n  "origin": "158.174.122.199, 158.174.122.199"\n}\n'
Profile exists
USE\u TOR=False

Normal IP: b'{\n  "origin": "my ip"\n}\n'
Profile does not exist. Http error 404

*
“我的ip”与Tor不同。

尝试使用instaloader加载配置文件。如果您没有得到错误配置文件,则存在错误配置文件。你可以用try-catch

#instagram.py
from instaloader import Instaloader
from instaloader import Profile

L = Instaloader()
profile = Profile.from_username(L.context, "amit")
#output <Profile amit (27235560)>

profile = Profile.from_username(L.context, "dasjkhkdhsjkahdjkashdadkajksdha")
#yields error.so you know profile doesnot exist

#instagram.py
从instaloader导入instaloader
从instaloader导入配置文件
L=Instaloader()
profile=profile.from_用户名(L.context,“amit”)
#输出
profile=profile.from_username(L.context,“dasjkhkdhsjkahdjkashdadkajksdha”)
#产生错误。所以您知道配置文件不存在

请包括您的终端输出too@Amit输出added@SaSha什么包是导入袜子?当尝试测试代码时,我得到一个ModuleNotFoundError。@CalebGoodman安装socks在终端中运行:
pip3安装请求[socks]
pip3安装pysocks
@SaSha我已经安装了所有这些软件包,但仍然收到相同的错误。你确定
import socks
在你这边有效吗?我想这可能是《谢谢》中的
。但这没有帮助。此代码被重定向到Instagram登录页面,不管配置文件是否存在。因此我无法区分现有/不存在的配置文件。请检查编辑。您可以通过instaloader实现这一点。不会有帮助,因为此库也会遇到“太多需求”,与我当前使用的urllib相同。Instagram仅在达到限制时向我发送登录页面。主要问题是如何作弊。它是每个ip,所以我需要更改ip。我知道如何使用Tor或proxy更改IP,但在这种情况下,instagram会立即将我重定向到登录页面,而不是用户页面或404。我的目标是准确地解决Tor/代理问题。而且,
请求
库以某种方式开始返回真实的用户页面,因此我删除了
请求
无效的信息。我在许多项目中使用instaloader。我通常从未遇到过任何问题。我想你需要发送大量的请求。如果您想更改每个请求的代理,或者可能要旋转代理。您可以在环境变量中设置http代理。instaloader使用引擎盖下的“请求”库。因此,它将使用来自环境变量的代理。我想你可以试试这种方法。