【发布时间】: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