我想从像http://openinnovation.cn/node/****这样的urls中从此页获得地址
这里有一个片段:
<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“
我尝试过几种方法,但都失败了,其中一种方法是失败的。我是个新手,只知道如何选择类、ids和其他我靠在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非常感谢!
发布于 2014-05-20 09:17:25
取决于您希望选择的节点的具体程度,您可以使用
.views-row > span:nth-of-type(2) a若要选择第二个跨度中的链接,请执行以下操作
a[href*='//openinnovation.cn/node/']若要选择其href属性中包含特定字符串的所有链接,请执行以下操作。这使用了attribute*='string'属性选择器,您可以阅读有关这里的更多信息。CSS没有XPath强大,所以您不能直接选择href属性。您必须使用lxml API显式地从e获取属性:
infoURLs = page_html.cssselect("a[href*='//openinnovation.cn/node/']")
for urlNode in infoURLs:
print urlNode.get("href")https://stackoverflow.com/questions/23752157
复制相似问题