我一直在使用python3.8及其module-urllib。
我的目标是获取文字
“”“为5000万用户打造的Dapps-关于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-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-2”没有任何结果。
最想要的问题是我如何获取文字?
第二个问题是,为什么“ xpath-2”不给我结果?
这是网址:https : //www.dapp.com/community
谢谢大家提前回答。
您必须更深入地获取文本。如果您只想要第一个标题:
(//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":").+?(?=")'))
输出:
否则,只需获取网页脚本标签(类型= text / javascript)中的json并使用适当的工具对其进行解析:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句