【问题标题】:Beautifulsoup - Extract text from next div sub tag based on previous div sub tagBeautifulsoup - 根据前一个 div 子标签从下一个 div 子标签中提取文本
【发布时间】:2019-02-26 01:07:57
【问题描述】:

我正在尝试根据之前的 div-span 文本提取下一个 div 跨度中的数据。下面是 html 内容,

<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:37px; top:161px; width:38px; height:13px;"><span style="font-family: b'Times-Bold'; font-size:13px">Name
<br></span></div><div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:85px; top:161px; width:58px; height:13px;"><span style="font-family: b'Helvetica'; font-size:13px">Ven
    <br></span></div>

我试图使用,

n_field = soup.find('span', text="Name\")

然后尝试使用从下一个兄弟获取文本,

n_field.next_sibling()

但是,由于字段中的“\n”,我无法找到跨度和提取 next_sibling 文本。

简而言之,我正在尝试以以下格式形成字典,

{"Name": "Ven"}

对此的任何帮助或想法表示赞赏。

【问题讨论】:

    标签: python python-3.x python-2.7 beautifulsoup python-beautifultable


    【解决方案1】:

    您可以使用re 代替bs4

    import re
    
    html = """
        <div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:37px; top:161px; width:38px; height:13px;">
            <span style="font-family: b'Times-Bold'; font-size:13px">Name
                <br>
            </span>
        </div>
        <div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:85px; top:161px; width:58px; height:13px;">
            <span style="font-family: b'Helvetica'; font-size:13px">Ven
                <br>
            </span>
        """
    
    mo = re.search(r'(Name).*?<span.*?13px">(.*?)\n', html, re.DOTALL)
    print(mo.groups())
    
    # for consecutive cases use re.finditer or re.findall
    html *= 5
    mo = re.finditer(r'(Name).*?<span.*?13px">(.*?)\n', html, re.DOTALL)
    
    for match in mo:
        print(match.groups())
    
    for (key, value) in re.findall(r'(Name).*?<span.*?13px">(.*?)\n', html, re.DOTALL):
        print(key, value)
    

    【讨论】:

    • 这行得通,但是在一个文件中有很多 div,我需要从中构建一个 dicts 列表,你的建议是什么
    • 如果他们遵循您描述的模式,您应该可以使用re.findall(r'(Name).*?&lt;span.*?13px"&gt;(.*?)\n', html, re.DOTALL)
    • 知道了,我观察到的是,有时有\n,有时没有,我想将其设为可选会起作用,让我尝试确认。
    • 然后修改正则表达式为(Name).*?&lt;span.*?13px"&gt;(.*?)[\n|&lt;]
    • 谢谢,它成功了,我只是稍微修改了一下,甚至可以检测到以开头的那些,这里是 mo = re.search(r'(\b'+ key + ')。 *?(.*?)[\n|
    【解决方案2】:

    我尝试了一下,出于某种原因,即使在删除 \n 之后,我也无法获取 nextSibling(),因此我尝试了不同的策略,如下所示:

    from bs4 import BeautifulSoup
    
    """Lets get rid of the \n""" 
    html = """<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:37px; top:161px; width:38px; height:13px;"><span style="font-family: b'Times-Bold'; font-size:13px">Name<br></span></div><div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:85px; top:161px; width:58px; height:13px;"><span style="font-family: b'Helvetica'; font-size:13px">Ven<br></span></div>""".replace("\n","")
    soup = BeautifulSoup(html)
    span_list = soup.findAll("span")
    result = {span_list[0].text:span_list[1].text.replace(" ","")}
    

    结果如下:

    {'姓名': '文'}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多