如何使用美丽的汤和蟒蛇从网站中提取文本

时间:2017-09-22 02:15:10

标签: python web-scraping beautifulsoup

我有这个html标签,我正试图刮

<span class="title NSNTitle">
      <small class="text-primary"><strong>
      ID 1040-KK-143-6964, 1040001436964
      </strong></small>
         <br>
      <small class="text-primary">
          MODIFICATION KIT,
     </small>
</span>

我使用此代码

   page_soup = soup(page_html, "html.parser")
   FSGcontainer = page_soup.find("h1", {"class": "nopad-top"}).find_all("small", {"class": "text-primary"})
   for subcontainer in FSGcontainer:
        FSGsubcard = subcontainer

        if FSGsubcard is not None:
            Nomenclature = FSGsubcard.text

            print(Nomenclature)

我得到了这个输出

  NSN 1040-KK-143-6964, 1005009927288


                                    MODIFICATION KIT,

我真正想要的是文本&#34;修改工具包,&#34; 我怎样才能捕获文本而不是ID?

2 个答案:

答案 0 :(得分:0)

small与选择第二个nomenclature = page_soup.find("h1", {"class": "nopad-top"} ).select_one( 'small:nth-of-type(2)' ).text.strip() 元素的css选择器一起使用。

$(window).on('unload', function() {
    $.ajax({
        type: 'POST',
        url: 'ajax/whattodo.php',
        async: false,
        data: {
            method: 'iquit',
            exitdata1: exitdata1,
            exitdata2: exitdata2
        }
    });
});

答案 1 :(得分:0)

试试这个。它可以让你获取你想要的特定项目。

for item in soup.find_all(class_="title"):
    text_item = item.find_all(class_="text-primary")[1].text
    print(text_item)

结果:

MODIFICATION KIT