If I knew the url of your site, I would have used it for example,
for this, I use https://www.weather.gov/
load the web site into chrome or firefox.
highlight the text you are interested in and right click
choose inspect element, move cursor in inspect over text node:
paste into code like (your xpath will be dfferent):
for this, I use https://www.weather.gov/
load the web site into chrome or firefox.
highlight the text you are interested in and right click
choose inspect element, move cursor in inspect over text node:
<strong>Travel date:</strong> 2019.10.10<br>right click --> copy --> XPath
paste into code like (your xpath will be dfferent):
xpath = '/html/body/div[5]/div/div[4]/p/a[2]'Now run code like:
from lxml import html import requests import sys def get_stuff(): page = None response = requests.get('https://www.weather.gov/') if response.status_code == 200: page = response.content else: print("c'ant load page") sys.exit(-1) tree = tree = html.fromstring((page)) # replace with your xpath node = tree.xpath('/html/body/div[4]/div[2]/div[1]/div[2]/div/div[2]/p') text = node[0].text.strip() print(text) if __name__ == '__main__': get_stuff()results:
Output:A slow moving storm system will bring a continued threat for heavy snow over the Rockies, heavy rain, flooding,and severe weather over the Plains into midweek. Over the Gulf of Mexico, Tropical Storm Michael is expected tostrengthen into a hurricane and cause direct impacts to the northeast Gulf Coast by midweek. Heavy rain from Michael could once again impact the Carolinas late week.