Hello!
I need to pull out links to sections of this site.
<li id="menu-item-28" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-28"><a href="http://worldagnetwork.com/category/community/">Community</a></li> <li id="menu-item-25" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-25"><a href="http://worldagnetwork.com/category/crops/">Crops</a></li> <li id="menu-item-27" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-27"><a href="http://worldagnetwork.com/category/livestock/">Livestock</a></li> <li id="menu-item-24" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-24"><a href="http://worldagnetwork.com/category/technology/">Technology</a></li> <li id="menu-item-26" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-26"><a href="http://worldagnetwork.com/category/business/">Business</a></li> <li id="menu-item-29" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-29"><a href="http://worldagnetwork.com/category/policy/">Policy</a></li> <li id="menu-item-53" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-53"><a href="http://worldagnetwork.com/category/environment/">Environment</a></li> <li id="menu-item-82" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-82"><a href="http://worldagnetwork.com/category/rd/">R&D</a></li> Do not quite understand how to use xpath.
This is what I do:
from lxml import etree import requests from io import StringIO, BytesIO import lxml.html as LH url = 'http://worldagnetwork.com/' headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36'} result = requests.get(url, headers=headers) tree = LH.document_fromstring(result.content) print(tree.xpath('//div/ul/li')[0].get('href')) print(tree.xpath('//div/ul')) already displays an empty list.
Help to understand, please.