AttributeError:“ WebElement”对象没有属性“ extract_first”

时间:2018-07-16 16:52:45

标签: python-3.x selenium web-scraping

我正在尝试运行以下脚本以从网页中提取标签并将其保存到csv文件中。

详细来说,我想提取与类名关联的标签。

但是,我遇到此错误:AttributeError:

'WebElement' object has no attribute 'extract_first'. 

脚本如下:

import csv
from selenium import webdriver
from time import sleep
from parsel import Selector
from selenium.webdriver.common.keys import Keys
from collections import defaultdict
from selenium.webdriver.support.select import Select

####### reading from the input file ##########

columns = defaultdict(list)  # each value in each column is appended to a list

# get the list of keywords from the csv file
with open('query.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)  # read rows into a dictionary format
for row in reader:  # read a row as {column1: value1, column2: value2,...}
    for (k, v) in row.items():  # go over each column name and value
        columns[k].append(v)  # append the value into the appropriate list

# the list containing all of the keywords
search_query_list = columns['Keyword']

########## start scraping ###############

rb_results = []

# create a driver and let it open google chrome
driver = webdriver.Chrome("chromedriver")

# get website
driver.get('https://www.redbubble.com/')
sleep(0.5)

for i in range(len(search_query_list)):

next_query = search_query_list[i]

# get RB website
driver.get('https://www.redbubble.com/')

# get the search by its id
search_bar = driver.find_element_by_name("query")

sleep(0.5)

# enter the query to the search bar
search_bar.send_keys(next_query)

# press enter
search_bar.send_keys(Keys.RETURN)
sleep(1)


# from parsel's selector get the page source
sel1 = Selector(text=driver.page_source)
sleep(0.5)

# get first shirt //
continue_link = driver.find_element_by_class_name('shared-components-ShopSearchSkeleton-ShopSearchSkeleton__composedComponentWrapper--1s_CI').click()
sleep(1)

sel2 = Selector(text=driver.page_source)
sleep(0.5)

################## get TAGS ###############

# Check tags for all products
try:
    # get the tags for the search query   
    tags_rb = driver.find_element_by_class_name("shared-components-Tags-Tags__listContent--oLdDf").extract_first()
    tags_rb = str(tags_rb)
       # if number of products is found print it and search for the prime
    # print the number of products found
    if tags_rb == None:
        rb_results.append("0")

    else:

        #rb_results = str(tags_rb)
        rb_results.append(tags_rb)

except ValueError:
   pass

#except:
 #rb_results.append("errore")   

###### writing part ########
with open ("rb_results.csv","w", newline='') as resultFile: 
    writer = csv.DictWriter(resultFile, fieldnames=["Rb Results"],delimiter='\t')
    writer.writeheader()

    writer.writerows({'Rb Results': item} for item in rb_results)

    resultFile.close()

关于如何解决它并提取shared-components-Tags-Tags__listContent--oLdDf文本的任何想法?非常感谢!

1 个答案:

答案 0 :(得分:1)

如果我理解正确,则需要元素文本。所以你可以这样:

替换:

tags_rb = driver.find_element_by_class_name("shared-components-Tags-Tags__listContent--oLdDf").extract_first()

具有:

tags_rb = driver.find_element_by_class_name("shared-components-Tags-Tags__listContent--oLdDf").text

您遇到此错误:

'WebElement' object has no attribute 'extract_first'.

因为WebElement没有方法.extract_first()

PS: ,您不需要:

tags_rb = str(tags_rb)

要替换的代码块是:

# Check tags for all products
try:
    # get the tags for the search query   
    tags_rb = driver.find_element_by_class_name("shared-components-Tags-Tags__listContent--oLdDf").text # get text
    # tags_rb = str(tags_rb) no need in this line
       # if number of products is found print it and search for the prime
    # print the number of products found
    if tags_rb == None:
        rb_results.append("0")

    else:

        #rb_results = str(tags_rb)
        rb_results.append(tags_rb)

except ValueError:
   pass