I'm having trouble with the second r.html.xpath request. When there is a special deal on an item, the second Xpath changes from
//*[@id="priceblock_ourprice"]
to
//*[@id="priceblock_dealprice"]
This causes the script to fail since there the right xpath cannot be returned. How can I include this second xpath that only shows up occasionally? I would like to see if either xpath exists, if so return that, or return N/A. The first url that is searched has the ourprice xpath and the second url has the dealprice xpath. What am I missing here?
from requests_html import HTMLSession
import pandas as pd
urls = ['http://amazon.com/dp/B01KZ6V00W',
'http://amazon.com/dp/B089FBPFHS'
]
def getPrice(url):
s = HTMLSession()
r = s.get(url)
r.html.render(sleep=1,timeout=20)
product = {
'title': str(r.html.xpath('//*[@id="productTitle"]', first=True).text),
'price': str(r.html.xpath('//*[@id="priceblock_ourprice"]', first=True).text),
'details': str(r.html.xpath('//*[@id="detailBulletsWrapper_feature_div"]', first=True).text)
}
res = {}
for key in list(product):
res[key] = product[key].replace('\n',' ')
print(res)
return res
prices = []
for url in urls:
prices.append(getPrice(url))
df = pd.DataFrame(prices)
print(df.head(15))
df.to_csv("testfile.csv",index=False)
print(len(prices))
traceback
'price': str(r.html.xpath('//*[@id="priceblock_ourprice"]', first=True).text),
AttributeError: 'NoneType' object has no attribute 'text'