慕蓋茨4494581
2022-12-06 16:39:39
我試圖為 Instagram 上的熱門標簽找到相關標簽,但在使用 BeautifulSoup 時卻一無所獲import requestsimport html5libimport csvfrom bs4 import BeautifulSoupdef list_of_tags(tags): related_tags = [] tmp = [] #for el in tags: url = "https://www.instagram.com/explore/tags/love/" req = requests.get(url) soup = BeautifulSoup(req.content, 'html5lib') print(soup) r_tag = soup.find('div', attrs = {'class' : 'WSpok'})我已經在其他網站上使用類似的代碼進行了抓取,并且取得了成功。但是在 Instagram 上嘗試時,我沒有得到任何 HTML 內容我嘗試調用特定的 div,但沒有用。我還有其他使用 JSON 請求的方法,但我想知道如何改進這個版本。提前致謝
1 回答

慕桂英546537
TA貢獻1848條經驗 獲得超10個贊
我終于用json做到了
import requests
import html5lib
import json
import time
import csv
from bs4 import BeautifulSoup
def list_of_tags(tags):
related_tags = []
for el in tags:
url = "https://www.instagram.com/explore/tags/"+ el +"/?__a=1"
req = requests.get(url)
data = json.loads(req.text)
edges = data['graphql']['hashtag']['edge_hashtag_to_related_tags']['edges']
for item in edges:
related_tags.append(item['node']['name'])
print(related_tags)
它將為您提供與您要查找的標簽相關的所有標簽
希望對某人有幫助。
添加回答
舉報
0/150
提交
取消