我一直在使用python3.8及其模块-urllib。
我的目标是拿到短信
“为5000万用户而建的Dapps--你应该知道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
谢谢你们提前给我答案。

发布于 2020-03-22 19:22:38
你必须走得更深才能拿到短信。如果你只想要第一个标题:
(//div[@class="article-sec"]//div[@class="name"])[1]/text()如果你想要所有的标题:
//div[@class="article-sec"]//div[@class="name"][1]/text()编辑:没有Selenium,在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":").+?(?=")'))产出:

否则,只需将json定位在网页的脚本标记(type = text/javascript)中,并使用适当的工具解析它:

https://stackoverflow.com/questions/60800032
复制相似问题