【问题标题】:How to grab specific text from a span given a specific heading如何从给定特定标题的跨度中获取特定文本
【发布时间】:2019-12-04 00:09:53
【问题描述】:

我正在尝试解析一个网站并从中获取所谓的 PSC 代码。该网站的 PSC 代码结构如下:

<span class="results_title_text">PSC (Code): </span>
</td>
<td width="30%">
<span class="results_text">
                MED &amp; SURGICAL INSTRUMENTS,EQ &amp; SUP
                (
                                  <a alt="Click here to drill down by PSC Code 6515" href="?q=60854+PRODUCT_OR_SERVICE_CODE%3A%226515%22&amp;s=FPDS.GOV&amp;templateName=1.5.1&amp;indexName=awardfull&amp;x=0&amp;y=0" title="Click here to drill down by PSC Code  6515">6515</a>
                )
                </span>
</td>
</tr>
<tr>

到目前为止,我已经编写了找到带有文本“PSC(代码):”的跨度的代码,但现在我不确定如何到达包含实际 PSC 代码的下一个跨度。这是我到目前为止所拥有的:

html_page = urllib.request.urlopen(url)
soup = BeautifulSoup(html_page, features='lxml')
#print(soup)
span = soup.findAll('span', {'class': 'results_title_text'})
for s in span:
    if s.text == 'PSC (Code): ':
        print(s)

这段代码只是在 html 中的任何地方打印“PSC(代码):”。关于如何进行的任何想法?

【问题讨论】:

  • 跨度封闭链接是否总是class="results_text"
  • 是的,它总是这样列出的。
  • 你有没有尝试过? SO 不是代码编写服务——如果你熟悉漂亮的汤,你就可以自己快速编写。请参考find_all
  • 您好,我确实编写了一些代码,但功能极少。我会用我的代码重写问题
  • 你想要的输出到底是什么?

标签: python html beautifulsoup tags


【解决方案1】:

还有其他方法可以搜索。假设与代码的链接足够相似,您可以使用正则表达式搜索:

>>> import re
>>> soup.find(title=re.compile(r'PSC Code')).text
'6515'
>>> soup.find(href=re.compile(r'PRODUCT_OR_SERVICE_CODE')).text
'6515'
>>> soup.find('a',href=re.compile(r'PRODUCT_OR_SERVICE_CODE')).text
'6515'
>>> soup.find('a',title=re.compile(r'PSC Code')).text
'6515'
>>> 

如果内容中有多个,您可以使用 .find_all 并迭代结果。

【讨论】:

    【解决方案2】:

    使用BeautifulSoup,您可以查看find_nextfind_all_next 方法。

    span = soup.find("span", {"class": "results_title_text"})
    if span.text.strip() == "PSC (Code):":
        l_other = span.find_all_next(string=True)
        for l in l_other:
            print(l)
    

    结果是

                    MED & SURGICAL INSTRUMENTS,EQ & SUP
                    (
    
    6515
    
                    )
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      怎么样?

      from simplified_scrapy.simplified_doc import SimplifiedDoc 
      html = '''<td><span class="results_title_text">PSC (Code): </span>
      </td>
      <td width="30%">
      <span class="results_text">
                      MED &amp; SURGICAL INSTRUMENTS,EQ &amp; SUP
                      (
                                        <a alt="Click here to drill down by PSC Code 6515" href="?q=60854+PRODUCT_OR_SERVICE_CODE%3A%226515%22&amp;s=FPDS.GOV&amp;templateName=1.5.1&amp;indexName=awardfull&amp;x=0&amp;y=0" title="Click here to drill down by PSC Code  6515">6515</a>
                      )
                      </span>
      </td>
      </tr>
      <tr>
      '''
      doc = SimplifiedDoc(html)
      span = doc.getElementByClass('results_title_text') # use class
      span = doc.getElementByText('PSC (Code):',tag='span') # use text
      print (span.text)
      
      nextSpan = span.getParent().getNexts()[0].span # Through parent-child structure
      print (nextSpan.html)
      
      nextSpan = doc.getElementByClass('results_text',start='class="results_title_text"') # Through class and location
      print (nextSpan.html)
      

      结果是

      PSC (Code):
      MED &amp; SURGICAL INSTRUMENTS,EQ &amp; SUP
                      (
                                        <a alt="Click here to drill down by PSC Code 6515" href="?q=60854+PRODUCT_OR_SERVICE_CODE%3A%226515%22&amp;s=FPDS.GOV&amp;templateName=1.5.1&amp;indexName=awardfull&amp;x=0&amp;y=0" title="Click here to drill down by PSC Code  6515">6515</a>)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        • 2022-06-15
        相关资源
        最近更新 更多