How can i crawl web data that not in tags

2019-07-31 05:21发布

问题:

<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.