I an trying to scrape a share price from yahoo finance. I don't understand Xpaths too well so I don't know how to access the value that is returned
from lxml import html
import requests
r = requests.get('https://uk.finance.yahoo.com/quote/BVXP.L?p=BVXP.L')
root = html.fromstring(r.content)
price = root.xpath('//*[@id="quote-header-info"]/div[3]/div/div/span[1]')
I've checked the xpath using XpathHelper in chrome, and it returns the value I'm looking for (3,325.00 - or whatever is the quote at that point.)
However in Python, I don't know how to access that info.
print(price)
# Returns [<Element span at 0x108832278>]
What's the correct way of doing it?
try print(price[0].text)
, you see price is a list with one item, and just by indexing we access the info you wanted.
a little FYI, if you ever get stuck always try to look at what methods it possess by print(dir(price[0]))
this is just an illustration from your example, but just by looking at the methods and attr of the object it gives us a glimpse, where to ahead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.