【问题标题】:instagram detect python requestsinstagram 检测 python 请求
【发布时间】:2019-02-01 19:32:21
【问题描述】:

我使用 python 进行网页抓取,但 instagram 检测设备,例如:
设备·Python 请求·x 城市,x
并阻止连接, 我该如何解决?

我尝试使用fake_useragentbrowser manual setting 代码:

def login(self):
    print ('Trying to login as %s...\n' % (self.username))
    self.s.headers.update({
        'Accept': '*/*',
        'Accept-Encoding' : 'gzip, deflate',
        'Accept-Language' : 'en-US;q=0.6,en;q=0.4',
        'authority': 'www.instagram.com',
        'ContentType' : 'application/x-www-form-urlencoded',
        'Connection': 'keep-alive',
        'Host' : 'www.instagram.com',
        'origin': 'https://www.instagram.com',
        'Referer': 'https://www.instagram.com',
        'Upgrade-Insecure-Requests':'1',
        'UserAgent':self.ua.random,
        'x-instagram-ajax':'1',
        'X-Requested-With': 'XMLHttpRequest'
    })
    r = self.s.get('https://www.instagram.com/') 
    self.s.headers.update({'X-CSRFToken' : r.cookies.get_dict()['csrftoken']})
    r = self.s.post('https://www.instagram.com/accounts/login/ajax/', data={'username':self.username, 'password':self.password}, allow_redirects=True)
    self.s.headers.update({'X-CSRFToken' : r.cookies.get_dict()['csrftoken']})
    loginstatus = json.loads(r.text)
    if loginstatus['authenticated'] == True :
        print ('Login Success')
        self.login_status=True
        return True
    elif loginstatus['authenticated'] == False :
        return False

【问题讨论】:

  • UserAgent 更改为User-Agent

标签: python python-requests instagram


【解决方案1】:

其中包含很多 javascript。使用 python requests,您可以直接从网络服务器获得 HTTP 响应。它必须在浏览器中呈现,才能从页面中获取所有信息和内容。

解决这个问题最简单的方法是使用selenium.

【讨论】:

    【解决方案2】:

    你可以尝试不同的东西。尝试使用您的浏览器之一复制User-Agent,如果它不起作用可能是因为它正在使用一些cookie或localStorage来存储有关登录的信息。尝试检查浏览器上的 cookie,按域过滤它们并将值复制到字典中。

    cookies = {"key": "value"} #Dictionary as cookies
    credentials = {'username': username, 'password': password}
    r = requests.post("http://examples.com",
            cookies=cookies, 
            data=credentials,
            allow_redirects=True)
    

    【讨论】:

    • 这比 selenium 快得多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 2019-02-03
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多