【问题标题】:Web scraping using python to print class div使用python进行网页抓取以打印类div
【发布时间】:2020-06-26 06:19:45
【问题描述】:

我想将它们全部打印为顶部 div 中给定站点的一类 div。这将是我有兴趣打印的网站 html 的一部分

<div class="game">
  <div class="history-feed__collection">
     <div class="history-feed__card h-card h-card_sm h-card_spades" style="width: 41px; margin-right: 18px; opacity: 1;">
         <div class="h-card__sign">9</div></div>
     <div class="history-feed__card h-card h-card_sm h-card_hearts" style="width: 41px; margin-right: 18px; opacity: 1;">
         <div class="h-card__sign">K</div></div>
     <div class="history-feed__card h-card h-card_sm h-card_diamonds" style="width: 41px; margin-right: 18px; opacity: 1;">
         <div class="h-card__sign">Q</div></div>
     <div class="history-feed__card h-card h-card_sm h-card_clubs" style="width: 41px; margin-right: 18px; opacity: 1;">
         <div class="h-card__sign">2</div>
</div></div>

Eu gostaria que o programa imprimisse assim: “历史-feed__card h-card h-card_sm h-card_spades, history-feed__card h-card h-card_sm h-card_hearts, ……”

我启动了这段代码,但我仍然发现问题, 因为代码只打印 Div 中包含的内容,而不是其类的名称

from selenium import webdriver

driver = webdriver.Chrome(executable_path='C:\chromedriver')

driver.get('https://card.com')

id = driver.find_elements_by_xpath('//*[@class]')

for ii in id:
    print(ii.get_attribute('class="hilo-history-feed__collection"'))
    
driver.close()

【问题讨论】:

  • 嗨 Gustavo,仅在手机上,但您尝试过美味的汤:crummy.com/software/BeautifulSoup/bs4/doc 吗?你可能想使用‘find_all(“div”)’,然后是‘get(“class”)’。
  • 看起来他们正在使用 Bootstrap...
  • 我会看看文档来完成我的项目,谢谢

标签: python html web-scraping


【解决方案1】:

尝试美丽的汤:

import requests
from bs4 import BeautifulSoup
URL = 'http://www.card.com'
response = requests.get(URL)
soup = BeautifulSoup(response.content, 'html5lib')
divs = soup.find_all('div')
classes = [div.get('class') for div in divs]
print(classes)

【讨论】:

    【解决方案2】:

    我设法用这段代码获得了成功

    
    import requests
    from bs4 import BeautifulSoup
    
    URL = 'http://www.card.com'
    response = requests.get(URL)
    soup = BeautifulSoup(response.content, 'html5lib')
    
    for i in soup.find_all('div'): 
        print(i)
    

    感谢所有帮助过的人

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 1970-01-01
      • 2020-10-04
      • 2021-05-08
      • 2018-07-20
      • 2021-01-13
      • 2020-03-13
      • 2016-02-10
      相关资源
      最近更新 更多