首页 文章

如何存储Selenium WebDriver中存在的webdriverObject.get_attribute(“srcset”)的返回值?

提问于
浏览
0

我正在用Python创建一个Selenium WebDriver剪贴板脚本来废弃来自Instagram的数据,这将帮助我创建一个深度学习问题的数据集 . 我既不能存储 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

以上代码的输出是[你,你,你,你,你,你,你,你,你,你,你,你,你,'' ',你',你',你',你',你',你',你',你',你',你',你'这是一个清单空白的Unicodes .

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

1 回答

  • 1

    尝试使用以下内容:

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

    希望它能帮到你!

相关问题