【问题标题】:Get an ID value held within a tag, but varies in each of the same class tag, using BeautifulSoup使用 BeautifulSoup 获取标签中保存的 ID 值,但在每个相同的类标签中有所不同
【发布时间】:2017-06-28 14:30:49
【问题描述】:

我正在抓取一个网站,我所追求的数据保存在 div 类中:

<div class="cat 108070288">

我可以将这些提取为容器使用

containers = soup.findAll("div", {"class" : re.compile("cat.*")})

这为我提供了我需要的所有容器。

我追求的是“猫号”。所以我这样做:

for container in containers:
    print container.get("class")

这会产生一个像这样的列表:

[u'cat', u'119482588']
[u'cat', u'119317618']
[u'cat', u'109582818']
[u'cat', u'119089813']
[u'cat', u'118819718']
[u'cat', u'118728418']
[u'cat', u'106407623']

如何只获取 ID?即,

119482588
119317618
109582818
119089813
118819718
118728418
106407623

【问题讨论】:

    标签: navigation beautifulsoup tags urllib


    【解决方案1】:

    我刚刚想通了:

    container.get("class")[1]
    

    给我

    u'106407623'
    

    这就是我所追求的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-10
      • 2023-04-08
      • 2021-11-27
      • 1970-01-01
      • 2017-12-02
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多