首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从页面中获取特殊的url?

如何从页面中获取特殊的url?
EN

Stack Overflow用户
提问于 2014-05-20 06:07:56
回答 1查看 74关注 0票数 0

我想从像http://openinnovation.cn/node/****这样的urls中从此页获得地址

这里有一个片段:

代码语言:javascript
复制
<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身上的东西。

代码语言:javascript
复制
infoURL = page_html.cssselect(".views-field views-field-nothing, .field-content, a.attrib['href']")

以下是相关的功能:

代码语言:javascript
复制
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

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-20 09:17:25

取决于您希望选择的节点的具体程度,您可以使用

代码语言:javascript
复制
.views-row > span:nth-of-type(2) a

若要选择第二个跨度中的链接,请执行以下操作

代码语言:javascript
复制
a[href*='//openinnovation.cn/node/']

若要选择其href属性中包含特定字符串的所有链接,请执行以下操作。这使用了attribute*='string'属性选择器,您可以阅读有关这里的更多信息。CSS没有XPath强大,所以您不能直接选择href属性。您必须使用lxml API显式地从e获取属性:

代码语言:javascript
复制
infoURLs = page_html.cssselect("a[href*='//openinnovation.cn/node/']")
for urlNode in infoURLs:
    print urlNode.get("href")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23752157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档