find_element_by_xpath何の属性を持っていない、とすべてが、私は少し異なるフォーマットされているページに実行されるまで、これまでswimmingly行くことのようです。パイソン - セレンはAttributeError:リストオブジェクトは、私はウェブサイトからの栄養データのいくつかのスクレイピングを実行しようとしています
[]
[]
[]
[]
[]
をしかし、私は要素の位置を定義した場合、それは正常に動作します:
values = browser.find_elements_by_class_name('size-12-fl-oz' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value')
印刷がこれを返します:セレンと、このような行を使用して
、空のリストを返します。
kcal = data.find_elements_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=1]").text
私が遭遇した問題は、要素がページごとに同じではない場合です私は繰り返す。 divの位置9に存在しないのであれば、エラーがスローされます。私はtry/catch
を行うために戻って、私のコードを編集しようとすると
は今、私が取得しています:
AttributeError: 'list' object has no attribute 'find_element_by_xpath'
またはここ
は私のコメントアウト地域からで、コードです私の前と後のテスト。AttributeError: 'list' object has no attribute 'find_elements_by_xpath'
import requests, bs4, urllib2, csv
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
browser = webdriver.Firefox()
...
#Loop on URLs to get Nutritional Information from each one.
with open('products.txt') as f:
for line in f:
url = line
# url = 'http://www.tapintoyourbeer.com/index.cfm?id=3'
browser.get(url)
with open("output.csv", "a") as o:
writeFile = csv.writer(o)
browser.implicitly_wait(3)
product_name = browser.find_element_by_tag_name('h1').text.title() #Get product name
size = browser.find_element_by_xpath("(//div[@class='dotted-tab'])").text #Get product size
data = browser.find_elements_by_xpath("//table[@class='beer-data-table']")
# values=[]
# values = browser.find_elements_by_class_name('size-12-fl-oz' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value')
try:
# values = data.find_elements_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])")
kcal = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=1]").text
kj = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=3]").text
fat = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=5]").text
carbs = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=7]").text
protein = data.find_element_by_xpath("(.//div[@class='size-12-fl-oz nutrition-value' or 'size-330-ml hide nutrition-value' or 'size-8-fl-oz nutrition-value'])[position()=9]").text
values = [kcal, kj, fat, carbs, protein]
print values
writeFile.writerow([product_name] + [size] + values)
except NoSuchElementException:
print("No Protein listed")
browser.quit()
リストを作成してCSVに出力していましたが、時々位置カウントが間違っていました。
[u'Budweiser', u'12 FL OZ', u'145.00', u'', u'', u'', u'']
[u"Beck'S", u'12 FL OZ', u'146.00', u'610.86', u'0.00', u'10.40', u'1.80']
[u'Bud Light', u'12 FL OZ', u'110.00', u'460.24', u'0.00', u'6.60', u'0.90']
[u'Michelob Ultra', u'12 FL OZ', u'95.00', u'397.48', u'0.00', u'2.60', u'0.60']
[u'Stella Artois', u'100 ML', u'43.30', u'KCAL/100 ML', u'181.17', u'KJ/100 ML', u'0.00']
特定のページに位置9が存在しない場合に問題が発生しました。
は、この頭痛を解決する方法上の任意の提案はありますか?私は別のページ&サイズの設定例を持っている必要がありますか?
私は助けていただきありがとうございます。
恐ろしいです。ありがとうございました。 – PDGill