簡體   English   中英

Python Instagram 網絡爬蟲問題

[英]Python Instagram Web scraper troubles

我正在嘗試構建一個網絡爬蟲,它告訴我在 Instagram 上使用主題標簽的次數,但我不斷收到不同迭代的錯誤代碼或當前響應的“無”。 這是我的代碼和 html。

Python

import requests
from bs4 import BeautifulSoup
url = 'https://www.instagram.com/explore/tags/savethekids/'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
tag = soup.find("span", {"class": "g47SY "})
print(tag)

那是我做的代碼

HTML

<span class="-nal3 ">
  <span class="g47SY ">22,922</span> 
   " posts"
</span>

那是來自 Instagram 的 HTML

如果任何真正知道他們在做什么的人都可以指出我做錯了什么以及如何解決它,那就太好了。

嘗試這個,

import requests

url = 'https://www.instagram.com/explore/tags/savethekids/?__a=1'

response = requests.get(url)

count = response.json().get('graphql', {}).get('hashtag', {}).get('edge_hashtag_to_media', {}).get('count')

print(count)

輸出:

22924

這里看到它的行動

使用請求時的問題是尚未呈現 html。 嘗試遵循有關抓取 Instagram 的教程

這使用稱為 selenium 的工具從 instagram 獲取實際的 html。

當您使用 selnium webdriver 時,以下代碼應該會獲取您正在尋找的元素。

from selenium.webdriver import Chrome
browser = Chrome()
url = 'https://www.instagram.com/explore/tags/savethekids/'
browser.get(url)
print(browser.find_element_by_class_name('g47SY'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM