我面临的问题在下面的代码中,可能很常见。你知道吗
基本上,我想从一个页面的子节点中选择xpath,但它提供了整个页面上的所有xpath匹配。你知道吗
有什么好处?你知道吗
import lxml.html
def readHTML(arg):
ret = ""
ret = lxml.html.parse(arg)
return ret
soup = (readHTML("http://www.myScrapingSite.com/"))
subGroup = soup.xpath("//div[@class='colmask']")[0]
#i want this to only be the cities in subGroup but its
#giving me the cities on the entire page..what am I doing wrong?
cities = subGroup.xpath('//li/a')
urls = {}
#so basically I am building a dictionary that is a superset of the desired set
for city in cities:
print city.attrib['href']
urls[city.attrib['href']] = 1
for url in urls:
subGroup2 = readHTML(url)
问题是
//
表示相对于文档根,即使对于子组也是如此。您真正想要的可能是相对于当前节点的.//
下面是一个例子
您可以看到
//
返回两个tagb
条目,而.//
只返回当前节点内的条目。你知道吗相关问题 更多 >
编程相关推荐