首页 > 解决方案 > 问题 Scaping Instagram Hashtag 帖子使用具有特殊字符的主题标签计数

问题描述

所以我可以用下面的代码刮掉 instagram hashtag 帖子的数量。

from selenium import webdriver

driver = webdriver.Firefox()

ig_link = 'https://www.instagram.com/explore/tags/100x35/'

driver.get(ig_link)

# Scrape Posts Count
posts_count = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/header/div[2]/div[1]/div[2]/span/span').text

print(posts_count)
driver.close()

我遇到的问题是当哈希标签中有一个标志时,例如:

https://www.instagram.com/explore/tags/100x35/ _

from selenium import webdriver

driver = webdriver.Chrome()

ig_link = 'https://www.instagram.com/explore/tags/100x35/'

driver.get(ig_link)

# Scrape Posts Count
posts_count = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/header/div[2]/div[1]/div[2]/span/span').text

print(posts_count)
driver.close()

我收到以下错误:

消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"// [@id="react-root"]/section/main/header/div[2]/div [1]/div[2]/span/span"}*

标签: pythonselenium-webdriver

解决方案


我相信您需要对 URL 进行编码以获取标志的 UTF-8 代码。在这种情况下,您可以更换

%F0%9F%87%B5%F0%9F%87%B7

获取纯文本 URL 并产生相同的结果。如果您要手动执行此操作,此工具应该很有用:link

如果你想在 python 中自动完成,urllib 也有一个工具

>>> import urllib.parse
>>> query = 'Hellö Wörld@Python'
>>> urllib.parse.quote(query)
'Hell%C3%B6%20W%C3%B6rld%40Python'

更多关于这里


推荐阅读