- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 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;"> </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>
我需要访问不同的 <dl>
<div id="glossary-list" class="list">
中的标签.
我的代码如下:
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"})
并打印出来,我得到:<div class="list" id="glossary-list"></div>
.在我看来,div 的内部以某种方式被阻塞,但我不太确定。
有没有人知道如何访问它?
最佳答案
第一个解决方案 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)
you can use
browser.close()
to close the browser
输出:
这是通过聊天处理所有用户请求的最终代码:
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)
关于python - 美丽汤 : how to show the inside of a div that won't show?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59090591/
关闭。这个问题是off-topic .它目前不接受答案。 想要改进这个问题? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我正在在线开发哈佛 CS50 的 PSET3,并且在 CS50 十五场游戏的“获胜”功能方面遇到问题。 该函数适用于 4x4 网格拼图,但不适用于 3x3 对于 3x3 的情况,该函数返回 true
我在 pset 的最后一部分,每当我使用 ./fifteen 3 #include #include #include // b
我正在学习 Javascript 并开始了第一个游戏元素:井字游戏。 我的元素及其代码: Tic Tac Toe Project 如果用户在井字游戏中获胜,我希望能够显示通知。 现在我只想测试我的“获
我刚刚将 MAMP 3 升级到 MAMP 4,而 MySQL 不再启动。 错误日志文件一遍又一遍地写着: 160905 11:22:17 mysqld_safe mysqld restarted 20
我目前正在构建一款非常流行的 Tic-Tac-Toe 游戏的复制品,该游戏使用纯 ruby 构建并经过测试的 TDD 风格。 我遇到的唯一问题是,在我的一生中,我无法弄清楚如何检查玩家何时获胜。我知道
我想构建 OSX 小部件以快速制作基于流数据的可视化仪表板组件的原型(prototype)。我想使用 d3.js 来进行显示,但是我尝试的最基本的事情都遇到了错误,我没有能力/耐心去解析。 在我开始在
我正在尝试在 android 中制作谷歌地图应用程序。我在 SDK 管理器中安装了 Google Play 服务。当我运行该应用程序时,模拟器中出现错误文本: App won't run unless
我的模拟器有问题,它不想启动,它阻止了加载屏幕。 最佳答案 我也遇到了这个问题,解决方法是打开模拟器,然后在顶部菜单中:Simulator > Reset content and setting。 关
我知道我需要一个 target="_blank,但它在我放置它的任何地方都不起作用。请帮忙! 最佳答案 你可以试试这个... 关于javascript - 添加目标 ="_blank to my
我正在用 PHP 使用 mySQL 编写一个模拟选举的网络应用程序。 我有三个表:Candidates、Elections 和 Votes。 Votes 包含 CandidateID、Election
在David Beazley's talk on generators ,他说,作为一个警告: Functions that consume an entire iterable won't term
我正在通过 Android Studio 使用 Android 模拟器来实现一个使用 Google map 的应用程序,但是当我运行该应用程序时,我在模拟器屏幕上收到以下消息: “如果您的设备不支持
我正在 Salesforce 中创建 DocuSign 自定义按钮。在运行 DocuSign 按钮逻辑之前尝试在某些字段值不满足时添加错误。 我想要的条件是,如果机会阶段的选择列表值不等于“已关闭”,
我正在尝试使用 BaseGameUtils 将游戏集成到我的应用程序中。一切看起来都很好,但是当我运行该应用程序时,我收到消息“在您更新 Google Play 服务之前,此应用程序不会运行”,当我单
我正在开发的网站上使用 jplayer html5 媒体播放器,但它无法在 Chrome 上播放某些 mp3。我试过在浏览器的 native 播放器中播放这些 mp3,但两者都无法播放。另一方面,当我
在 Programming Rust 一书中的 References Are Never 部分,他们指出没有与 C 的 NULL 类似的东西。那一点我明白了。然后他们说 Rust won't conv
Auto-Scaling 下的 EC2 可能由于内存问题而无法扩展。我的自动缩放设置会检查 CPU,因为默认情况下无法将内存添加到 cloudwatch 指标中。有什么建议可以让我的服务器在上述情况下
每当我启动 Azure Cloud Shell 时,都会收到此错误: 无法装载 azure 文件共享。您的云驱动器将不可用您的 Cloud Shell session 将是短暂的,因此任何文件或系统更
我在 UITableViewCell 中有一个灵活的可编辑 UITextView。一个简单项目的完整源代码可以在 https://github.com/AlexChekanov/TextViewInT
我是一名优秀的程序员,十分优秀!