【问题标题】:Scraping text; I'm not sure the Google Chrome Inspect element is giving me the correct XPath. Where can I get the correct path?刮文字;我不确定 Google Chrome Inspect 元素是否给了我正确的 XPath。我在哪里可以获得正确的路径?
【发布时间】:2020-04-03 06:43:15
【问题描述】:

在这里,我想抓取一个名为“fundsnetservices.com”的网站。具体来说,我想抓取每个程序下方的文本——它是关于一个段落的文本。

使用 Google Chrome Inspect 方法,我能够拉出这个...

'/html/body/div[3]/div/div/div[1]/div/p[2]/text()'

... 作为 xpath。但是,每次我打印出文本时,它都会返回 [ ]。为什么会这样?

response = urllib.request.urlopen('http://www.fundsnetservices.com/searchresult/30/International-Grants-&-Funders/18.html')
tree = etree.HTML(response.read().decode('utf-16'))
text = tree.xpath('/html/body/div[3]/div/div/div[1]/div/p[2]/text()')

【问题讨论】:

    标签: python html xml xpath lxml


    【解决方案1】:

    您的代码似乎返回了空白节点。更正您的 XPath:

    //p[@class="tdclass"]/text()[3]
    

    【讨论】:

    • 是否低于我合并您的修订的方式?它对我来说仍然不太有效:'/html/body/div[3]/div/div/div[1]/div/p[2]/text()//p[@class="tdclass"]/ text()[3]' 顺便说一句,我也刚刚意识到我要访问的一些文本恰好是尾文本。
    • 不用担心;我解决了问题!我意识到如何使用 BeautifulSoup。 :)
    • 不错。 tree.xpath('//p[@class="tdclass"]/text()[3]') 就够了。:)
    猜你喜欢
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多