How can i crawl web data that not in tags

Deadly 提交于 2020-01-24 21:30:06

问题


<div id="main-content" class="content">
<div class="metaline">
<span class="article-meta author">jorden</span>
</div>
 "
 1.name:jorden> 
 2.age:28

  --
 "
 <span class="D2"> from 111.111.111.111 </span>
  </div>

I only need

1.name:jorden
2.age:28

xxx.select('#main-content') this will return all things, but i only need part of them. Because they are not in any tags, i don't know how to do.


回答1:


You want to find the tag before the text in question (in your case, <div class="metaline">) and then look at the next sibling in the HTML parse tree:

text = soup.find("div", class_='metaline').next_sibling
print(text)
# "
# 1.name:jorden> 
# 2.age:28
#
#  --
# "
# 

Once you get the raw text, strip it, etc.



来源:https://stackoverflow.com/questions/44124862/how-can-i-crawl-web-data-that-not-in-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!