gpt4 book ai didi

python - Instagram 抓取

转载 作者:行者123 更新时间:2023-12-05 05:02:03 35 4
gpt4 key购买 nike

以下代码在计算机上运行以从 Instagram 帐户中抓取数据。当我尝试在 VPS 服务器上使用它时,我被重定向到 Instagram 登录页面,因此脚本不起作用。

为什么当我在电脑上或服务器上时,Instagram 的 react 不一样?

wget也是一样。在计算机上我有个人资料页面,在服务器上我被重定向到登录页面。

import requests
import re


class InstagramScraper:
"""
Scraper of Instagram profiles infos.
"""

def __init__(self, session: requests.Session, instagram_account_name: str):
self.session = session
self._account_name = self.clean_account_name(instagram_account_name)
self.load_data()

def load_data(self):
#print(self._account_name)
response = self.session.get("https://www.instagram.com/{account_name}/".format(account_name=self._account_name))
#print(response)
#print(response.text)
publications_regex = r'"edge_owner_to_timeline_media":{"count":(\d*),'
self._publications = re.search(publications_regex, response.text).group(1)

followers_regex = r'"edge_followed_by":{"count":(\d*)'
self._followers = re.search(followers_regex, response.text).group(1)

# title_regex = r'"@type":".*","name":"(.*)",'
title_regex = r'"full_name":"(.*)",'
self._title = re.search(title_regex, response.text).group(1)
self._title = self._title.split('\"')[0]

following_regex = r'"edge_follow":{"count":(\d*)}'
self._following = re.search(following_regex, response.text).group(1)

def clean_account_name(self, value) -> str:
"""
Return the account name without the url address.
"""
found: str = re.search("https://www.instagram.com/(.*)/", value)
if found:
return found.group(1)
return value

@property
def publications(self) -> int:
"""
Number of publications by this account.
"""
return self._publications

@property
def followers(self) -> int:
"""
Number of followers of this account.
"""
return self._followers

@property
def title(self) -> str:
"""
Name of the Instagram profile.
"""
return self._title

@property
def account(self) -> str:
"""
Account name used on Instagram.
"""
return self._account_name

@property
def following(self) -> int:
"""
Number of accounts this profile is following.
"""
return self._following

def __str__(self) -> str:
return str({
'Account': self.account,
'Followers': self.followers,
'Publications': self.publications,
'Following': self.following,
'Title': self.title,
})


if __name__ == "__main__":
with requests.session() as session:
scraper = InstagramScraper(session, "https://www.instagram.com/ksc_lokeren/")
print(scraper)

最佳答案

可能是因为您在计算机上使用自己的凭据登录? furas 提到了一个黑名单,但如果您以前从未在此服务器上运行过它,我对此表示怀疑。

为了避免这种情况,我可以使用 headless 浏览器,它模拟普通浏览器并让您在网站上导航。您将使用您的凭据模拟登录,然后从 cookie 中检索 csrftoken 和 sessionid 并关闭浏览器。

我是用 javascript 做的,所以我不能给你看,但逻辑是这样的:

  1. 创建 headless 浏览器

  2. 将请求的“接受语言” header 设置为“en-US”

  3. 导航到 https://www.instagram.com/accounts/login/ .等到空闲

  4. 使用您的凭据模拟登录。寻找:

    'input[name="password"]'//用于密码。

    'input[name="username"]'//用于用户名。

    'button[type="submit"]'//用于登录按钮

  5. 等到空闲

  6. 获取所有 cookie 并检索 csrftoken 和 sessionid

  7. 关闭 headless 浏览器

然后,在向 https://www.instagram.com/{account_name}/ 发出任何请求时,不要忘记在请求 header 中设置 csrftoken 和 sessionid cookie。一段时间后它会过期,你需要重新启动

关于python - Instagram 抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62408588/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com