我想在此页面上为每个div获取文章网址:https://www.google.com/trends/home/all/IN 我能够获得图像链接和标题,但对于文章链接,它给出了
Traceback (most recent call last):
File "google.py", line 25, in getGooglerends
print s.find_elements_by_class_name('image-wrapper').get_attribute('href')
AttributeError: 'list' object has no attribute 'get_attribute'
代码:
driver = webdriver.PhantomJS('/usr/local/bin/phantomjs')
driver.set_window_size(1124, 850)
driver.get("https://www.google.com/trends/home/all/IN")
trend = {}
def getGooglerends():
try:
#Does this line makes any sense
#element = WebDriverWait(driver, 20).until(lambda driver: driver.find_elements_by_class_name('md-list-block ng-scope'))
for s in driver.find_elements_by_class_name('md-list-item-block'):
print s.find_element_by_tag_name('img').get_attribute('src')
print s.find_element_by_tag_name('img').get_attribute('alt')
print s.find_elements_by_class_name('image-wrapper').get_attribute('href')
except:
import traceback
print traceback.format_exc()
getGooglerends()
有关从锚标记获取文章链接的任何建议吗?
答案 0 :(得分:1)
WebDriver.find_elements_by_class_name
返回元素列表,而不是单个元素。
s.find_elements_by_class_name('image-wrapper')
^
使用WebDriver.find_element_by_class_name
代替WebDriver.find_elements_by_class_name
。
s.find_element_by_class_name('image-wrapper')