【问题标题】:BeautifulSoup : how to show the inside of a div that won't show?BeautifulSoup:如何显示不会显示的 div 内部?
【发布时间】:2019-11-28 14:00:35
【问题描述】:

我是 BeautifulSoup 的新手,我有一些我不明白的问题,我认为这个问题可能尚未得到解答,但在这种情况下,我找到的答案都没有帮助我。

我需要访问 div 的内部来检索网站的词汇表条目,但是使用 BeautifulSoup 时,该 div 的内部似乎根本“不显示”。你能帮帮我吗?

所以这是网站上的html:

<!DOCTYPE html>
<html lang="en-US" style="margin-top: 0px !important;">
<head>...</head>
<body>
<header>...</header>
<section id="glossary" class="search-off">
    <dl class="title">
        <dt>Glossary</dt>
    </dl>
    <div class="content">
        <aside id="glossary-aside">
            <div></div>
            <ul></ul>
        </aside>
        <div id="glossary-list" class="list">
          <dl data-id="2103">...</dl>
          <dl data-id="1105">
            <dt>ABV (Alcohol by volume)</dt>
            <dd>
              <p style="margin-bottom: 0cm; text-align: justify;"><span style="font-family: Arial Cyr,sans-serif;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="font-size: medium;">Alcohol by volume (ABV) is the measure of an alcoholic beverage’s alcohol content. Wines may have alcohol content from 4% ABV to 18% ABV; however, wines’ typical alcohol content ranges from 12.5% to 14.5% ABV. You can find a particular wine’s alcohol content by checking the label.</span></span></span></span><span style="font-size: medium;">&nbsp;</span></p>
            </dd>
          </dl>
          <dl data-id="1106">...</dl>
          <dl data-id="1213">...</dl>
          <dl data-id="2490">...</dl>
          <dl data-id="11705">...</dl>
          <dl data-id="1782">...</dl>
        </div>
        <div id="glossary-single" class="list">...</div>
    </div>
    <div class="s_content">
        <div id="glossary-s_list" class="list"></div>
    </div>
</section>
<footer></footer>
</body>
</html>

我需要访问&lt;div id="glossary-list" class="list"&gt; 中不同的&lt;dl&gt; 标签。

我的代码现在如下:

url_winevibe = requests.get("http://winevibe.com/glossary")
soup = BeautifulSoup(html, "lxml")
ct = url_winevibe.find("div", {"id":"glossary-list"}).findAll("dl")

我尝试了各种方法,包括找到后代和孩子,但我得到的只是一个空列表。

如果我尝试ct = soup.find("div", {"id":"glossary-list"}) 并打印它,我会得到:&lt;div class="list" id="glossary-list"&gt;&lt;/div&gt;。在我看来,div 的内部以某种方式被阻塞了,但我不太确定。

有人知道如何访问它吗?

【问题讨论】:

  • 某些元素是由脚本动态生成的,不会出现在您的bs4 上。您需要使用不同的包,如 requests-htmlselenium 可以在解析这些元素之前渲染它们。

标签: python beautifulsoup


【解决方案1】:

第一个解决方案url 是基于我对数据加载位置的研究!我确实看到它是通过XHR 从不同的url 加载的,JavaScript 呈现:

import requests
import json

r = requests.get('http://winevibe.com/wp-json/glossary/key/?l=en').json()
hoks = json.loads(r)
for item in hoks:
  print(item['key'])

第二种解决方案:

from selenium import webdriver
from bs4 import BeautifulSoup
import time

browser = webdriver.Firefox()
url = 'http://winevibe.com/glossary/'
browser.get(url)
time.sleep(20)  # wait 20 seconds for the site to load.
html = browser.page_source
soup = BeautifulSoup(html, features='html.parser')
for item in soup.findAll('div', attrs={'id': 'glossary-list'}):
    for dt in item.findAll('dt'):
        print(dt.text)

您可以使用browser.close()关闭浏览器

输出:

这是通过 Chat 处理所有用户请求的最终代码:

import requests
import json

r = requests.get('http://winevibe.com/wp-json/glossary/key/?l=en').json()
data = json.loads(r)
result = ([(item['key'], item['id']) for item in data])
text = []
for item in result:
    try:
        r = requests.get(
            f"http://winevibe.com/wp-json/glossary/text/?id={item[1]}").json()
        data = json.loads(r)
        print(f"Getting Text For: {item[0]}")
        text.append(data[0]['text'])
    except KeyboardInterrupt:
        print('Good Bye')
        break

with open('result.txt', 'w+') as f:
    for a, b in zip(result, text):
        lines = ', '.join([a[0], b.replace('\n', '')]) + '\n'
        f.write(lines)

【讨论】:

  • 这绝对是我想要的,但如果我想获得整个词汇表的解释,我该怎么做?
  • @Basile print(r) 如果这就是你要找的。或者让我知道是否不是
  • 很抱歉,我只收到&lt;Response [200]&gt;print(r)。但除此之外,非常感谢您的回答,这正是我所需要的!
  • @αԋɱҽԃαмєяιcαη,干得好。我喜欢你的解决方案。我总是从你的帖子中学到一些东西
猜你喜欢
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多