【问题标题】:Navigate HTML Tags to Extract text from Anchored Tags导航 HTML 标签以从锚定标签中提取文本
【发布时间】:2019-06-19 16:53:35
【问题描述】:

我需要从网页中提取某些文本,但文本所在的锚标记嵌入在几个子类中。

我是网络抓取的新手,如果这个场景已经得到解答,我很抱歉,但我需要从这个网页 (https://www.astm.org/search/fullsite-search.html?query=alloy&toplevel=products-and-services&sublevel=standards-and-publications) 抓取文本,我尝试使用 bs4 解析页面,但是在创建汤对象时我无法从每个单独的结果中获取标签。

使用 requests 和 bs4 我试过了

    page = requests.get(url)
    soup = BeautifulSoup(page.text)
    print(soup)

它会输出找到我需要的文本的标签

    <div class ="span8 main searchresults">
    <div id="results"></div>

我希望看到结果 id 中的标签应该看起来像

    <div id="results">
    <div class="res">
    <div class="resTable">
    <h4 class="resTitle>
    <a...

我需要从每个结果中获取标题文本,例如第一个文本是

    "ASTM A506-16 Standard Specification for Alloy and Structural Alloy Steel, Sheet and Strip, Hot-Rolled and Cold-Rolled"

问题是当我尝试在 bs4 中引用任何这些标签时,没有返回任何内容。我如何通过这些类来获取标签中的文本?

【问题讨论】:

    标签: python web-scraping


    【解决方案1】:

    您的数据似乎以 JSON 格式编码在 HTML 页面中(BeautifulSoup 无法帮助您,但您可以使用 re 模块提取数据):

    import re
    import json
    import requests
    from pprint import pprint
    
    url = 'https://www.astm.org/search/fullsite-search.html?query=alloy&toplevel=products-and-services&sublevel=standards-and-publications'
    
    data = json.loads(re.findall(r'var mc_results = ({.*?})\s*;', requests.get(url, verify=False).text, flags=re.DOTALL)[0])
    
    for s in data['resSet']:
        for result in s['results']:
            pprint(result['res']['meta'])
            print('*' * 80)
    

    打印:

    {'gs_designation': 'A506',
     'gs_homebook': '0103 CS01',
     'gs_year': '16',
     'mc_addtocart': 'PDF-A506',
     'mc_date': '2016',
     'mc_dltype': 'allstd,active,basecompass',
     'mc_doctype': 'Active Standard',
     'mc_doi': 'A0506-16',
     'mc_icsdata': '77.140.50 (Flat steel products and semi-products)',
     'mc_keywordsen': 'cold-rolled steel products~ hot-rolled steel products~ ',
     'mc_language': 'English',
     'mc_login': 'true',
     'mc_maincat': 'standard,sedl',
     'mc_maincomm': 'A01',
     'mc_relatedurl': 'A506_related.htm',
     'mc_section': '01',
     'mc_sectors': 'Metals',
     'mc_sublevel': 'standards-and-publications,sedl-digital-library',
     'mc_suburl': '/SUBSCRIPTION/filtrexx40.cgi?+/usr6/htdocs/newpilot.com/SUBSCRIPTION/REDLINE_PAGES/A506.htm',
     'mc_tax0': 'Properties_and_Measurements,Test_Methods,Materials',
     'mc_tax1': 'Chemical_Properties,Mechanical_Test,Metals_--_Iron_and_Alloys',
     'mc_tax2': 'Chemical_Composition,Fractography,Tensile_Test,Steel,Iron_and_Steel_Products',
     'mc_tax3': 'Hardness_Test,Alloy_Steel,Flat_Products,Specialty_Steel',
     'mc_tax4': 'Structural_Steel',
     'mc_taxkeywordsen': 'Alloy Steel,Flat Products,Structural Steel,Chemical '
                         'Composition,Hardness Test,Tensile Test',
     'mc_tertiary': 'standards-products',
     'mc_toplevel': 'products-and-services',
     'mc_unspscdata': '30264100(Steel alloy sheets)',
     'title': 'Standard Specification for Alloy and Structural Alloy Steel, Sheet '
              'and Strip, Hot-Rolled and Cold-Rolled'}
    ********************************************************************************
    {'gs_designation': 'B768',
     'gs_homebook': '0201 CS02',
     'gs_year': '11(2016)',
     'mc_addtocart': 'PDF-B768',
     'mc_date': '2016',
     'mc_dltype': 'allstd,active,basecompass',
     'mc_doctype': 'Active Standard',
     'mc_doi': 'B0768-11R16',
    
    ...and so on.
    

    【讨论】:

    • 谢谢你这对我有用,你能解释一下你是如何知道数据在 json 中的以及你是如何访问它的吗?
    • @jgonz1 我没有用beautifulsoup 找到任何东西,所以我在Firefox 开发者工具中查看了HTML 页面的源代码。在那里我搜索了我在渲染页面上看到的数据。数据在&lt;script&gt;标签中,所以我知道它是JSON...我可以使用re来提取它。
    【解决方案2】:

    这是我为深入了解不同类所做的工作

    加载到beautifulsoup

    soup = BeautifulSoup(data.text, 'html.parser')
    

    在html代码中找到排行榜并解析出来

    FeaturedArticles = soup.findAll('article',{'class':'featured'})
    

    打印(精选文章)

    for Articles in FeaturedArticles: 
        title = Articles.a.text
        print(title)
    

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 1970-01-01
      • 2021-10-22
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多