python - 使用 BeautifulSoup 在 Python 中抓取特定的 div
问题描述
我目前正在尝试从网页(https://www1.president.go.kr/articles/8863)中抓取文本数据。我想要的部分位于[[[div class="text left cb text_wrap motion fadeIn visible" data-animation="fadeIn"]]]
. 我已经尝试过soup.find('div', {'class' :'text left cb text_wrap motion fadeIn visible'})
了,但我什么也没得到。
response = requests.get('https://www1.president.go.kr/articles/8863')
if response.status_code == 404:
print('404: A page could not load')
exit(1)
soup = BeautifulSoup(response.content, 'html.parser')
data = [] #class="text left cb text_wrap motion fadeIn visible"
html = soup.find('div', {'class' :'text left cb text_wrap motion fadeIn visible'})
# Not returning correctly.
if html is None:
print('it is not getting data')
exit(2)
我应该输入什么来获得那部分?
解决方案
你需要改变这个:
html = soup.find('div', {'class' :'text left cb text_wrap motion fadeIn visible'})
对此:
html = soup.find('div', {'class' :'text left cb text_wrap motion'})
因为如果您检查页面源,该 div 看起来像这样:
<div class="text left cb text_wrap motion" data-animation="fadeIn">
推荐阅读
- python - Bokeh HoverTool 显示“???” 即使在情节中更新了源
- azure - 如何在数据流活动中动态执行 SQL 查询
- solr - solr 核心中的未知土耳其字符
- php - 使用 Livewire 和 Echo 在 Laravel 中捕捉事件
- scala - 错误:未找到:scala 命令字数中的值
- akka - 如何通知特定类型的所有持久参与者
- c++ - unsigned 和 int 之间的整数提升
- apache-beam - 将 Python 脚本作为数据流作业提交
- mysql - 在选择中尝试一些子查询,总和的差异 - Doctrine 2.6.4
- vhdl - 在 VHDL 中,当作为参数传递给函数/过程时,无约束数组的索引范围默认为什么?