beautifulsoup,使用re.compile查找文本

时间:2017-07-12 22:29:45

标签: python beautifulsoup

为什么这找不到任何东西?我希望从这个HTML中提取id

from bs4 import BeautifulSoup
import re
a="""
<html lang="en-US">
 <head>
  <title>
   Coverage
  </title>
  <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
  <meta content="2017-07-12T08:12:00.0000000" name="created"/>
 </head>
 <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
  <div id="div:{1586118a-0184-027e-07fc-99debbfc309f}{35}" style="position:absolute;left:1030px;top:449px;width:624px">
   <p id="p:{dd73b86c-408c-4068-a1e7-769ad024cf2e}{40}" style="margin-top:5.5pt;margin-bottom:5.5pt">
    {FB} 2 Facebook 465.8 /
    <span style="color:green">
     12
    </span>
    <span style="color:green">
     5
    </span>
    <span style="color:green">
     10
    </span>
    <span style="color:red">
     -3
    </span>
    / updated
   </p>
  </div>
  </body>
</html>
"""
soup=BeautifulSoup(a,'html.parser')
ticker='{FB}'
target= soup.find('p', text = re.compile(ticker))

有多个p我刚才省略了其余部分。我需要text=部分

我还尝试过通配符(.*),但仍然可以使用它。

我必须通过自动收件人获取id ...我不知道其他任何内容,页面的其余部分是动态的

1 个答案:

答案 0 :(得分:1)

这将获得包含文字"id"的{​​{1}}代码的<p>值:

"{FB}"

更紧凑的方式:

ticker='{FB}'
target= soup.find_all('p')
for items in target:
    check=items.text
    if '{FB}' in check:
        print (items.get("id"))
相关问题