【问题标题】:How to store the return value of webdriverObject.get_attribute("srcset") present in Selenium WebDriver?如何存储 Selenium WebDriver 中存在的 webdriverObject.get_attribute("srcset") 的返回值?
【发布时间】:2018-12-04 23:18:11
【问题描述】:

我正在用 Python 制作一个 Selenium WebDriver scraper 脚本,以从 Instagram scrape 数据,这将帮助我为深度学习问题创建数据集。我既无法存储 webdriverObject.get_attribute() 的 Unicode 返回值,也无法将其转换为字符串。但令人惊讶的是,我能够打印这些值。

将 Unicode 转换为字符串后如何将结果存储到列表中?

下面是我的代码:

################################ import modules and set path ###############
from selenium import webdriver


path="C:\Users\User\Downloads\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(path)
from time import sleep

################################ login into instagram #######################
driver.get('https://www.instagram.com/accounts/login')
username = driver.find_element_by_xpath('//*[@name="username"]')
password = driver.find_element_by_xpath('//*[@name="password"]')
username.send_keys("username") #pass your username
password.send_keys("pass") #pass your password

sleep(3)

a=driver.find_element_by_css_selector("._5f5mN").click()

######################################## search for a hashtag###################
inpu=raw_input("Enter the hashtag: ")
url="https://www.instagram.com/explore/tags/"+inpu+"/?hl=en"
driver.get(url)

b=[]
for i in driver.find_elements_by_tag_name("img"):
    b.append(i.get_attribute("srcset"))
print b

上面代码的输出是[u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'',u'',u'',u'',u'',u'',u'',u'',u'',u'',u'',u''] 这是一个空白 Unicode 列表。

如果我现在更改代码的最后一部分并将b.append(i.get_attribute("srcset")) 替换为print i.get_attribute("srcset"),那么它似乎正在打印链接。

【问题讨论】:

    标签: python-2.7 selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    尝试使用以下内容:

    i.get_attribute("srcset").decode("utf-8")
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      相关资源
      最近更新 更多