【问题标题】:Web Scraping, How to extract data from two same tags using bs4 in pythonWeb Scraping,如何在 python 中使用 bs4 从两个相同的标签中提取数据
【发布时间】:2017-03-25 10:14:37
【问题描述】:

我正在使用带有 python 的 bs4 并尝试从网页中获取数据。 Link 我在我想要的信息上使用了检查元素,但两者都有相同的标签、类。

             <a class="cell__value" data-tracker-action="click" data-tracker-label="information_technology.01" href="/markets/sectors/information-technology">
             Information Technology
            </a>
           </div>
           <div class="cell__return">
            <div class="cell__label">
             % Price Change
            </div>
            <div class="cell__value" data-type="better">
             +0.05%
            </div>
           </div>
          </div>
          <div class="cell">
           <div class="cell__name">
            <div class="cell__label">
             Industry
            </div>
            <a class="cell__value" data-tracker-action="click" data-tracker-label="information_technology.02" href="/markets/sectors/information-technology">
             Software &amp; Services
            </a>
           </div>
           <div class="cell__return">
            <div class="cell__label">
             % Price Change
            </div>
            <div class="cell__value" data-type="worse">
             -0.04%
            </div>
           </div>
          </div>
         </div>

我是这样做的:

sect= soup.find("a",{"data-tracker-label":"information_technology.01"})
print sect.text
sect_per= soup.find("div",{"data-type":"worse"or"better"})
print sect_per.text
ind=soup.find("a",{"data-tracker-label":"information_technology.02"})
print ind.text
ind_per=soup.find("div",{"div",{"data-type":"worse"or"better"})
print ind_per

both print ind_per print ind_per 由于相同的班级和标签

我需要提取+0.05%-0.04%分别。 >

请给我建议。

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:
    soup = BeautifulSoup(example, "html.parser")
    
    for cell in soup.find_all("div", class_="cell"): 
        name = ""
        namecell = cell.find("a", class_="cell__value", text=True)
        if namecell is not None:
             name = namecell.get_text(strip=True)
        price_chage = cell.find("div", class_="cell__value").get_text(strip=True)
        print ( "%s: Price Change:  %s" % (name, price_chage))
    

    哪些输出:

    信息技术:价格变化:+0.05%

    软件和服务:价格变化:-0.04%

    您可以保存该值以供进一步处理。

    【讨论】:

    • 错误:name = cell.find("a", class_="cell__value").get_text(strip=True) AttributeError: 'NoneType' 对象没有属性 'get_text'
    • 感谢您的支持。非常感谢!
    • 还有一个问题@Zroq 如果你能帮忙的话。如何在 python 中将“截至 7:05 AM EDT 3/24/2017”转换为 yyyy/MM/dd hh:mm:ss
    • @AnujMasand datetime.strptime("As of 7:05 AM EDT 3/24/2017", "As of %H:%M %p EDT %m/%d/%Y")
    【解决方案2】:

    or如果左操作数为真值则返回左操作数(字符串为非空字符串):

    >>> "worse" or "better"
    'worse'
    

    所以,下面一行:

    ind_per = soup.find("div",{"div",{"data-type":"worse" or "better"})
    

    基本上与以下内容相同:

    ind_per = soup.find("div",{"div",{"data-type":"worse"})
    

    需要单独查询:

    ind_per = soup.find("div",{"div",{"data-type": "worse"})
    print ind_per
    ind_per = soup.find("div",{"div",{"data-type": "better"})
    print ind_per
    

    或使用for循环:

    for data_type in ('worse', 'better'):
        ind_per = soup.find("div",{"div",{"data-type": data_type})
        print ind_per
    

    【讨论】:

      【解决方案3】:
      <p class="sort-num_votes-visible">
      <span class="text-muted">Votes:</span>
      **<span data-value="2333089" name="nv">2,333,089</span>**
      <span class="ghost">|</span> <span class="text-muted">Gross:</span>
      **<span data-value="28,341,469" name="nv">$28.34M</span>**
      </p>
      

      “我想获得电影的票数和总票数 但两者都具有相同的名称是“nv”,所以我们对这些使用索引”

       vote_mov=container.findAll("span",{"name":"nv"})
          vote=vote_mov[0].text
          
          gross_mov=container.findAll("span",{"name":"nv"})
          gross=gross_mov[1].text
      

      “这是第一次获得投票,然后是总票数enter image description here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-21
        • 1970-01-01
        • 2021-06-12
        • 2021-09-16
        • 2016-06-03
        • 2017-06-28
        • 1970-01-01
        • 2022-01-18
        相关资源
        最近更新 更多