【问题标题】:BeautifulSoup incomplete children with find_allBeautifulSoup 不完整的孩子与 find_all
【发布时间】:2017-11-04 02:45:08
【问题描述】:

我正在准备从以下 HTML 脚本中废弃“product-tech-section-row”类下的嵌套 div 实例:

<h2 class="product-tech-section-title">
    Présentation de la TV SAMSUNG UE49MU9005</h2>

<div class="product-tech-section-row">
    <div>
        Désignation</b> :
    </div>
    <div>
        <b>SAMSUNG UE49MU9005</b> (UE 49MU9005 TXXC)<br><br>Plus d'informations sur les <a             href="http://www.lcd-compare.com/info-tv-led-samsung.htm" title="TV Samsung : informations et statistiques">TV LED Samsung</a><br><a href="http://www.lcd-compare.com/tv-liste-122.htm?tv_label=7,8" title="Liste des TV 4K">Voir les TV 4K (Ultra HD ou Quad HD)</a></div>
</div>


<div class="product-tech-section-row">
    <div>
        Date de sortie (approx.)</b> :
    </div>
    <div>
        Mars 2017</div>
</div>

但是,使用 find_all() 只会提取第一个 div 子项(仅 Désignation、SAMSUNG UE... 不会出现),如下面的代码所示。我错过了什么吗?非常感谢您的帮助。

from urllib.request import urlopen as uReq
from urllib.request import Request
from bs4 import BeautifulSoup as soup

#Allowing access to the website (personal use)
prod_url="http://www.lcd-compare.com/televiseur-SAMUE49MU9005-SAMSUNG-UE49MU9005.htm"
hdr = {'User-Agent': 'Mozilla/5.0'}
req = Request(prod_url,headers=hdr)
prod_html=uReq(req)

#Parsing the technical details
tec_list = prod_soup.find_all("div",{"class","product-tech-section-row"})

---------------------------------------------------------------------------------------
#However, this is what I am getting:
>>>print(tec_list[0])
<div class="product-tech-section-row">
<div>
Désignation</div></div>

>>>print(tec_list[0].findChildren())
[<div>
 Désignation<\div>]

【问题讨论】:

  • 尝试 print(tec_list[1]) 这将为您提供“SAMSUNG UE49MU9005”结果。请记住,find_all() 返回一个被删除的元素列表,该列表存储在 tec_list 中。
  • 感谢您的回复,很遗憾 print(tec_list[1]) 只会返回“Date de sortie (approx.)”,也就是下面的“product-tech-section-row”类跨度>
  • 嗨 p404 请在下面查看我的答案。

标签: python html web-scraping beautifulsoup findall


【解决方案1】:

我认为你不能废弃嵌套元素的原因是因为你访问的网站是由 Javascript 大量渲染的。

我已经使用 selenium 来验证是否是这种情况,并且我能够正常解析嵌套元素而没有问题。

代码:

from selenium import webdriver
from bs4 import BeautifulSoup

chromeOptions = Options()  
chromeOptions.add_argument("--headless")  
driver = webdriver.Chrome(chrome_options=chromeOptions)
url = 'http://www.lcd-compare.com/televiseur-SAMUE49MU9005-SAMSUNG-UE49MU9005.htm'
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
tec_list = soup.findAll("div",{"class","product-tech-section-row"})

print(tec_list[0])

输出:

<div class="product-tech-section-row">
<div>
Désignation :
</div>
<div>
<b>SAMSUNG UE49MU9005</b> (UE 49MU9005 TXXC)<br/><br/>Plus d'informations sur les <a data-hasqtip="139" href="http://www.lcd-compare.com/info-tv-led-samsung.htm" oldtitle="TV Samsung : informations et statistiques" title="">TV LED Samsung</a><br/><a data-hasqtip="141" href="http://www.lcd-compare.com/tv-liste-122.htm?tv_label=7,8" oldtitle="Liste des TV 4K" title="">Voir les TV 4K (Ultra HD ou Quad HD)</a></div>
</div>

【讨论】:

  • 感谢阿里!你的建议效果很好。顺便问一下,我想问你是否有其他库可以做同样的工作,但不涉及浏览器。这样就可以很容易地将这种代码添加到 Web API 中。
  • @p404,抱歉回复晚了。我真的不知道任何其他可以实现您目标的库。但请继续搜索。
  • 嗨,阿里,我做了一些研究,发现 PhantomJS 无头浏览器可以完成它。它也可以从 selenium webdriver 加载,例如:driver = webdriver.PhantomJS()。我希望您将来也能发现它有用。
  • 感谢您的回复,我实际上对 PhantomJS 很陌生,但我认为您想要 Selenium 的替代品。我很高兴你找到了解决方案。还有一种方法可以使用 ChromeDriver 运行无头浏览器。
  • 这个设置看起来很有趣,感谢更新!
猜你喜欢
  • 2017-06-07
  • 2014-05-08
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2021-01-22
  • 2019-12-20
  • 1970-01-01
  • 2014-03-26
相关资源
最近更新 更多