首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >BeautifulSoup -获取我正在迭代的div的属性

BeautifulSoup -获取我正在迭代的div的属性
EN

Stack Overflow用户
提问于 2019-03-01 08:13:11
回答 2查看 87关注 0票数 1

我正在使用BeautifulSoup来解析VC网站上的公司列表。我已经找到了要迭代的正确元素,但我似乎无法获得这些元素本身的数据。

下面是我正在浏览的示例HTML:

代码语言:javascript
复制
<div id="content" class="site-content">
    <main id="primary" class="content-area" role="main">
        <header class="page-header">
        <main id="portfolio-landing-company-list" class="page-content">
            <section id="portfolio__list--grid" class="portfolio__list--all">
            <div class="company company-stage--venturegrowth company-type--enterprise company--single-company">
                    <div class="company__thumbnail company__thumbnail-link">
                        <a href="http://www.domain1.com" title="Company1" target="_blank">
                    </div>      
            </div>
            <div class="company company-stage--seed company-type--bio company--single-company">
                    <div class="company__thumbnail company__thumbnail-link">
                        <a href="http://www.domain2.com" title="Company2" target="_blank">
                    </div>
            </div>

这就是我目前使用BeautifulSoup和的方式这部分工作起来很棒,

代码语言:javascript
复制
portfolio = soup.find('div', attrs={'class': 'portfolio-tiles'})
for eachco in portfolio.find_all('article'):
  companyname = eachco.a['title']
  companyurl = eachco.a['href']

但是我想要做的是从这里获取类元素,

代码语言:javascript
复制
<div class="company company-stage--venturegrowth company-type--enterprise company--single-company">
or
<div class="company company-stage--seed company-type--bio company--single-company">

(列表中的每个公司都有多个变体)

我尝试使用以下命令遍历:

代码语言:javascript
复制
portfolio = soup.find('div', attrs={'class': 'portfolio-tiles'})
for eachco in portfolio.find_all('article'):
  companyattributes = eachco.div['class']

但这就吐出了一排排:

代码语言:javascript
复制
['company__thumbnail', 'company__thumbnail-link']

(也就是,低于我想要的级别)

我怎么才能遍历所有的结果,却得到每个结果的类元素呢?我感觉我遗漏了一些非常基本的东西,但如果有人能帮我弄清楚那是什么,我将不胜感激!

更新

我最终选择了下面的方法,这使得所有的东西都在一起工作:

代码语言:javascript
复制
portfolio = soup.find_all('div', class_=re.compile("company company-"))
    for eachco in portfolio:
        coname = eachco.a['title']
        courl = eachco.a['href']
        cotypes = eachco['class']
        costage = cotypes[1]
        comarket = cotypes[2]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-01 08:41:15

您可以使用re模块来查找类元素中的特定文本。

代码语言:javascript
复制
from bs4 import BeautifulSoup
import re
html = """<html><div id="content" class="site-content">
    <main id="primary" class="content-area" role="main">
        <header class="page-header">
        <main id="portfolio-landing-company-list" class="page-content">
            <section id="portfolio__list--grid" class="portfolio__list--all">
            <div class="company company-stage--venturegrowth company-type--enterprise company--single-company">
                    <div class="company__thumbnail company__thumbnail-link">(
                        <a href="http://www.domain1.com" title="Company1" target="_blank">
                    </div>
            </div>
            <div class="company company-stage--venturegrowth company-type--enterprise company--single-company">
                    <div class="company__thumbnail company__thumbnail-link">
                        <a href="http://www.domain2.com" title="Company2" target="_blank">
                    </div>
            </div> </html>"""

soup = BeautifulSoup(html, 'html.parser')
divs = soup.find_all('div' ,class_=re.compile("stage"))
for div in divs:
    print(div['class'])

输出:

代码语言:javascript
复制
[u'company', u'company-stage--venturegrowth', u'company-type--enterprise', u'company--single-company']
[u'company', u'company-stage--venturegrowth', u'company-type--enterprise', u'company--single-company']
票数 1
EN

Stack Overflow用户

发布于 2019-03-01 08:51:33

I这就是你要找的:

代码语言:javascript
复制
for i in range(len(soup)):
     print(soup.select('div[class*="stage"]')[i].attrs['class'])

输出

代码语言:javascript
复制
   ['company', 'company-stage--venturegrowth', 'company-type--enterprise', 'company--single-company']
   ['company', 'company-stage--seed', 'company-type--bio', 'company--single-company']y--single-company']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54936196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档