我想从this page的http://openinnovation.cn/node/****
这样的URL获取地址:
下面是一个片段:
<div class="views-row views-row-2 views-row-even">
<span class="views-field views-field-title">
<span class="field-content">
<a href="http://simile.mit.edu/wiki/Babel" target="_blank">babel</a>
</span>
</span>
<span class="views-field views-field-nothing">
<span class="field-content"><a href="http://openinnovation.cn/node/9506">详细信息</a>
</span>
</span>
</div>
我想要的是这个字符串“http://openinnovation.cn/node/9506”
我试过几种方法,但都失败了,这是其中一种。我是一个新手,只知道如何选择类,ID和其他我从codecademy学习。你知道吗
infoURL = page_html.cssselect(".views-field views-field-nothing, .field-content, a.attrib['href']")
以下是相关功能:
def main():
for j in range(58,64):
listURL = 'http://www.openinnovation.cn/opentools/function/'+str(j)
listPage = urllib.urlopen(listURL)
listhtml = listPage.read()
page_html = lxml.html.fromstring(listhtml)
# get the information page url from the list page:
#infoURL = page_html.cssselect("a.ttrib['href']")
infoURL = page_html.cssselect(".views-field views-field-nothing, .field-content, a.attrib['href']")
for e in infoURL:
print e
非常感谢!你知道吗
取决于选择节点的具体程度,您可以使用
选择第二个跨距中的链接或
选择在其
href
属性中包含特定字符串的所有链接。这将使用attribute*='string'
属性选择器,您可以阅读有关here的更多信息。CSS没有XPath强大,因此不能直接选择href
属性。必须使用lxml API从e
显式获取属性:相关问题 更多 >
编程相关推荐