【问题标题】:BeautifulSoup doesn't shows all the elements in tagBeautifulSoup 没有显示标签中的所有元素
【发布时间】:2017-09-02 07:58:33
【问题描述】:

我正在尝试解析 this website 并使用 BeautifulSoup.find 在内容框 card__body 中获取有关 auto 的信息,但它没有找到所有类。我也试过webdriver.PhantomJS(),但也没有显示。

这是我的代码:

from bs4 import BeautifulSoup
from selenium import webdriver

url='http://www.autobody.ru/catalog/10230/217881/'

browser = webdriver.PhantomJS()
browser.get(url)
html = browser.page_source

soup = BeautifulSoup(html, 'html5lib') 
JTitems = soup.find("div", attrs={"class":"content-box__strong red-text card__price"})
JTitems

w = soup.find("div", attrs={"class":"content-box card__body"})
w

为什么这种方法不起作用?我应该怎么做才能获得有关汽车的所有信息?我正在使用 Python 2.7。

【问题讨论】:

  • 你确定这些元素在页面中,有这些特定的类吗?
  • 在问题中包含一些标记。
  • 网站中没有 content-box card__body 类的元素,如果您可以包含您想要提取的确切文本,它将对每个试图提供帮助的人有所帮助。
  • 我正在尝试提取此文本 - 2208р(红色)和下面的所有内容

标签: python parsing beautifulsoup


【解决方案1】:

找到您需要的信息所在的table。然后找到所有tr 并遍历它们以获取文本。

from bs4 import BeautifulSoup
from selenium import webdriver

url='http://www.autobody.ru/catalog/10230/217881/'

browser = webdriver.Chrome()
browser.get(url)
html = browser.page_source

soup = BeautifulSoup(html, 'lxml')

price = soup.find('div', class_='price').get_text()
print(price)

for tr in soup.find('table', class_='tech').find_all('tr'):
    print(tr.get_text())

browser.close()
browser.quit()

【讨论】:

  • 谢谢,它的工作和显示所有文章都很棒,但它不会显示看到红色价格 2208 р
  • @egorkh 我已经更新了我的代码,它也会打印价格。
  • 非常感谢您的帮助。很奇怪,当我尝试查看价格的 html 代码时,我看到了 -

    2208 Р

    ,但是你使用了类价格,你是如何得到这个类的?我怎么也能找到它?
  • 我是通过查看页面源代码找到的。我没有检查元素。
  • 看来我在解析方面绝对为零请告诉我,我该如何查看页面源代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2020-06-13
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多