Python 3:如何从包含多个类值的div Web抓取文本

时间:2018-09-06 01:14:43

标签: html python-3.x selenium web-scraping beautifulsoup

我正在尝试通过Web抓取网站(Here is the link to website),但页面中的div似乎具有多个类属性,这使我很难抓取数据。我试图查找发布在Stackoverflow上的历史问题,但是找不到我想要的答案。以下是我从网站提取的部分代码:

<div data-reactid="118">
  <div class="ue-ga base_ ue-jk" style="margin-left:-24px;margin-bottom:;" data-reactid="119">
    <div style="display: flex; flex-direction: column; width: 100%; padding-left: 24px;" data-reactid="120">
      <div class="ue-a3 ue-ap ue-a6 ue-gb ue-ah ue-n ue-f5 ue-ec ue-gc ue-gd ue-ge ue-gf base_ ue-jv ue-gz ue-h0 ue-h1" data-reactid="121">
        <div class="ue-a6 ue-bz ue-gb ue-ah ue-gg ue-gh ue-gi" data-reactid="122">
          <div class="ue-bn ue-bo ue-cc ue-bq ue-g9 ue-bs" title="Want to extract this part" data-reactid="123">
            Want to extract this part
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

我要提取的是内容为“ 要提取此部分”的文本。我确实考虑过通过data-reactid抓取数据,但是不同的页面分配了不同的data-reactid编号,所以这不是一个好主意。我还想告知类名不是唯一的。

有人可以引导我做这件事吗?非常感激。

5 个答案:

答案 0 :(得分:1)

您可以按以下方式使用jQuery

$("div[title=Want to extract this part]").text();

答案 1 :(得分:1)

如果每个页面上该特定元素的类始终保持相同,则可以使用此选择器作为目标:

.ue-bn.ue-bo.ue-cc.ue-bq.ue-g9.ue-bs

但是,您可以使用许多其他选择器,但这取决于它们在页面之间是否唯一且一致。

答案 2 :(得分:1)

菜单:

enter image description here
-所有要循环使用的菜单,css选择器:div.base_ h3
-按名称命名的菜单,xpath://div[contains(@class,'base_')]//h3[.='Big Mac® Bundles']

食品卡

enter image description here
-标题,css选择器:div[title]
-标题,xpath://div[./div[@title]]/div[@title]
-价格,xpath://div[./div[@title]]//span
如果要循环播放:

cards = driver.find_elements_by_xpath("//div[./div[@title]]")
for card in cards:
     title = card.find_element_by_css_selector("div[title]")
     price = card.find_element_by_css_selector("span")
     #or using xpath
     #title = card.find_element_by_xpath("./div[@title]")
     #price = card.find_element_by_xpath(".//span")

类别菜单:

enter image description here
-所有类别,css选择器:a[href*='category']

答案 3 :(得分:0)

这可能对您有帮助

from bs4 import BeautifulSoup
html = """<div data-reactid="118">
<div class="ue-ga base_ ue-jk" style="margin-left:-24px;margin-bottom:;" data-reactid="119">
<div style="display: flex; flex-direction: column; width: 100%; padding-left: 24px;" data-reactid="120">
  <div class="ue-a3 ue-ap ue-a6 ue-gb ue-ah ue-n ue-f5 ue-ec ue-gc ue-gd ue-ge ue-gf base_ ue-jv ue-gz ue-h0 ue-h1" data-reactid="121">
    <div class="ue-a6 ue-bz ue-gb ue-ah ue-gg ue-gh ue-gi" data-reactid="122">
      <div class="ue-bn ue-bo ue-cc ue-bq ue-g9 ue-bs" title="Want to extract this part" data-reactid="123">
        Want to extract this part
      </div>
    </div>
  </div>
</div>
</div>
</div>"""

soup = BeautifulSoup(html,'html.parser')
tag = soup.find('div', attrs={'class':'ue-bn'})
text = (''.join(tag.stripped_strings))
print (text)

答案 4 :(得分:0)

根据 HTML ,您共享了提取文本要提取此部分的功能,因为该元素是您必须引出React元素> WebDriverWait ,使元素可见,您可以使用以下任一解决方案:

  • 使用title属性:

    myText = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.base_ div[title]"))).get_attribute("title")
    
  • 使用innerHTML

    myText = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.base_ div[title]"))).get_attribute("innerHTML")
    

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC