Python 当使用xpath进行刮取时,我该怎么办
我一直在使用python3.8及其模块urllib 我的目标是得到文本 “”“为5000万用户构建的DAPP-关于ICON,您应该了解8个案例”“” 从下面的htmlPython 当使用xpath进行刮取时,我该怎么办,python,xpath,Python,Xpath,我一直在使用python3.8及其模块urllib 我的目标是得到文本 “”“为5000万用户构建的DAPP-关于ICON,您应该了解8个案例”“” 从下面的html <div class="article-sec" data-v-727113bd=""> <div data-v-3b154919="" data-v-727113bd="" class="item item"> <a data-v-3b154919="" href="/article/dapp
<div class="article-sec" data-v-727113bd="">
<div data-v-3b154919="" data-v-727113bd="" class="item item">
<a data-v-3b154919="" href="/article/dapp-com-list-icon" target="_blank">
<div data-v-3b154919="" class="image" style="background-image: url("https://dappimg.com/media/image/article/1fe42da2f8ca44dab2884690624ecfa7.jpg");"></div>
<div data-v-3b154919="" class="combine-info">
<div data-v-3b154919="" class="name">Dapps Built for 50 Million Users - 8 Cases You Should Know about ICON</div>
</div>
<div data-v-3b154919="" class="time">Mar 18 · 1220 Views</div>
</a>
</div>
xpath-1 : xpath('//div[@class="article-sec"]')
xpath-2 : xpath('//div[@class="article-sec"]/div')
xpath-1:xpath('//div[@class=“article-sec”]”)
xpath-2:xpath('//div[@class=“article sec”]/div')
“xpath-1”给了我一个元素。
但是“xpath-2”没有给出任何结果
最想问的问题是我怎样才能得到文本
第二个问题是为什么“xpath-2”不给我一个结果
以下是网址:
谢谢你们提前回答
你必须更深入地阅读文本。如果您只想要第一个标题:
(//div[@class="article-sec"]//div[@class="name"])[1]/text()
如果您想要所有标题:
//div[@class="article-sec"]//div[@class="name"][1]/text()
编辑:如果没有硒,在R中,您可以执行以下操作:
library(RCurl)
library(XML)
library(stringr)
page=getURL("https://www.dapp.com/community")
parse=htmlParse(page)
titles=xpathSApply(parse,"//div[@id='__nuxt']/following::script[@type]",xmlValue)
result=unlist(str_extract_all(gsub(',"influencers.*','',titles),'(?<="title":").+?(?=")'))
库(RCurl)
库(XML)
图书馆(stringr)
page=getURL(“https://www.dapp.com/community")
parse=htmlParse(第页)
titles=xpathsaply(解析“//div[@id='''.\u numxt']/following::script[@type]”,xmlValue)
result=unlist(str_extract_all(gsub(',“influencers.*','',titles),)(?您必须更深入地获取文本。如果您只想要第一个标题:
(//div[@class="article-sec"]//div[@class="name"])[1]/text()
如果您想要所有标题:
//div[@class="article-sec"]//div[@class="name"][1]/text()
编辑:如果没有硒,在R中,您可以执行以下操作:
library(RCurl)
library(XML)
library(stringr)
page=getURL("https://www.dapp.com/community")
parse=htmlParse(page)
titles=xpathSApply(parse,"//div[@id='__nuxt']/following::script[@type]",xmlValue)
result=unlist(str_extract_all(gsub(',"influencers.*','',titles),'(?<="title":").+?(?=")'))
库(RCurl)
库(XML)
图书馆(stringr)
page=getURL(“https://www.dapp.com/community")
parse=htmlParse(第页)
titles=xpathsaply(解析“//div[@id='''.\u numxt']/following::script[@type]”,xmlValue)
结果=未列出(str_extract_all(gsub(',“influencers.*”,'',titles),'(?将xml复制到问题中比发布图像要好得多,因为这样我们就可以对其进行实验。最好的是一个较小的被黑客攻击的exmaple xml,它演示了问题,但不会让我们搜索大量数据。请以文本格式提供HTML代码。当您获得'//div[@class=“article sec”]“
元素,打印出它的子树。内部文档可能是由javascript生成的,而不是urllib
获取的版本。在这种情况下,请尝试selenium
通过浏览器将页面拉入。@tdelaney我添加了一个我抓取的网页链接和html结构。谢谢您的提醒。我将尝试使用sEleniumcopy将xml复制到问题中比发布图像要好得多,因为这样我们就可以对其进行实验。最好的是一个较小的被黑客攻击的exmaple xml,它演示了问题,但不会让我们搜索大量数据。请以文本格式提供HTML代码。当您获得'//div[@class=“article sec”]“
元素,打印出它的子树。内部文档可能是由javascript生成的,而不是urllib
获取的版本。在这种情况下,请尝试selenium
通过浏览器将页面拉入。@tdelaney我添加了一个我抓取的网页链接和html结构。谢谢您的提醒。我将尝试使用sEleniumXPath表达式是正确的。如@tdelaney所述,您可能必须使用Selenium来获取数据。Post通过一些选项进行编辑。XPath表达式是正确的。如@tdelaney所述,您可能必须使用Selenium来获取数据。Post通过一些选项进行编辑。