【问题标题】:Beautiful soup: extracting xml value with cerain attribute美丽的汤:用 cerain 属性提取 xml 值
【发布时间】:2018-08-18 20:11:40
【问题描述】:

我正在使用this script 来抓取boardgamegeek。

这工作正常,正在从xml data 中获取信息

我想在输出 csv 中再提取一个元素。这个:

<items>
 <item>
   <link type="boardgamepublisher" id="1001" value="(Web published)"/>
   <link type="boardgamepublisher" id="1341" value="something else"/>
 </item>  
</items>

id 是随机的,我想要的是类型属性“boardgamepublisher”的链接标签的值,并将它们添加到 csv 字段(最好是一个单元格中的所有 boardgamepublisher)。有时是一个棋盘游戏发行商,有时更多。有很多链接元素,所以我需要按它们过滤

【问题讨论】:

    标签: xml web-scraping beautifulsoup


    【解决方案1】:
     soup = BeautifulSoup(req.content, 'xml')
        items = soup.find_all('item')
        for item in items:
    

    需要添加此代码才能添加发布者

     publishers=item.find_all(type="boardgamepublisher" )
            gpublishers=""
            for publisher in publishers:
               gpublishers += publisher["value"]+"," 
    

    第一行返回一个列表

       <link type="boardgamepublisher" id="1001" value="(Web published)"/>
    

       <link type="boardgamepublisher" id="1341" value="something else"/>
    

    publisher["value"] 提取 Value 属性的内容。

    我仍在寻找更好的建议,因为我担心这个解决方案会很慢。

    【讨论】:

      猜你喜欢
      • 2014-04-16
      • 2015-05-08
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 2015-10-21
      • 2020-01-19
      • 2021-04-03
      • 1970-01-01
      相关资源
      最近更新 更多