I expect the following code to return the text "In Stock" or "Out of Stock" (to check stock at an online store) but it returns only "[]". The XPath code was obtained from a browser's element inspector, and seems to be valid. I read online about namespaces possibly being the problem. Tips?
from lxml import html
import requests
url = 'http://www.thesource.ca/en-ca/computers-and-tablets/computer-accessories/mice/logitech-m310-wireless-mouse/p/2618659'
path = '//*[@id="content"]/section/section/div/font/div[7]/div/div[1]/div[2]/ul/li[1]/div/text()'
page = requests.get(url)
tree = html.fromstring(page.content)
stock = tree.xpath(path)
print(stock)
EDIT: Solution based on Padraic Cunningham's post.
Still not the most elegant due to its reliance on some absolute paths but at least this is working:
from lxml import html
import requests
import re
# in stock example URL
#url = 'http://www.thesource.ca/en-ca/computers-and-tablets/computer-accessories/mice/logitech-m310-wireless-mouse/p/2618659'
# out of stock example URL
url = 'http://www.thesource.ca/en-ca/computers-and-tablets/computer-accessories/mice/microsoft-basic-optical-mouse/p/108029878'
path = '//ul[@class="availability"]/li[./div[1]]'
inner_path = './div[1]/text()'
page = requests.get(url)
tree = html.fromstring(page.content)
stock = tree.xpath(path)
current = stock[0].xpath(inner_path)
print(current[0])
if re.search(r'in.*stock.*online', current[0], flags=re.IGNORECASE):
print "Success!"
else:
print "Keep waiting..."