无法使用BeautifulSoup4抓取网站

用户名

我要抓取的文字是标题123rd Meeting,来自

https://www.bcb.gov.br/zh/#!/c/copomstatements/1724

为此,我使用此代码

import urllib.request           #get the HTML page from url 
import urllib.error

from bs4 import BeautifulSoup


# set page to read
with urllib.request.urlopen('https://www.bcb.gov.br/en/#!/c/copomstatements/1724') as response:
   page = response.read()

# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page, "html.parser")
print(soup)

# Inspect: <h3 class="BCTituloPagina ng-binding">123rd Meeting</h3>
title = soup.find("h3", attrs={"class": "BCTituloPagina ng-binding"})
print(title)

但是,命令

print(soup)

既不返回标题:123rd Meeting,也不返回正文:鉴于....目标降低了25个基点。

您不能使用python中的常规请求库来提取标题,因为您要提取的元素是使用javascript呈现的。您将需要使用硒来实现您的目标。

码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('https://www.bcb.gov.br/en/#!/c/copomstatements/1724')
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//h3')))
title = driver.find_element_by_xpath('//h3').text
print(title)
driver.close()

输出:

123rd Meeting

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用BeautifulSoup4进行数据抓取的问题

使用BeautifulSoup网站抓取IMDb页面

使用python和Beautifulsoup4从抓取数据中写入和保存CSV文件

如何使用python和beautifulsoup4循环抓取网站中多个页面的数据

BeautifulSoup4无法从表中抓取数据

ImportError:无法导入名称“ BeautifulSoup4”

BeautifulSoup4无法从此表中抓取数据

BeautifulSoup4抓取不能超过网站的第一页(Python 3.6)

使用BeautifulSoup抓取网站时显示符号

从抓取的数据中分割html(Python + BeautifulSoup4)

如何使用BeautifulSoup4使用Python修复Web抓取中的错误

无法使用BeautifulSoup4(初学者)抓取正确的Wikitable

如何使用BeautifulSoup4从客户标签中抓取信息

无法使用python抓取网站

使用python + beautifulSoup4从动态图中抓取数据

循环不适用于使用python和beautifulsoup4抓取数据

使用BeautifulSoup抓取特定网站

使用python 2.7和beautifulsoup 4进行网站抓取

使用beautifulsoup4后如何分离抓取结果?

如何使用beautifulsoup4用我的python脚本抓取更多亚马逊产品?

在python 3.6中使用beautifulsoup4抓取网站以获取产品信息时

无法使用beautifulsoup抓取日本网站

使用beautifulsoup4从网站提取数据并解析成csv

在beautifulsoup4 中,当纯粹根据元素和其中的文本抓取网站时,如何返回多个结果?

无法使用 BeautifulSoup 从网站上抓取所有数据

使用 python 抓取网站 - BeautifulSoup

Beautifulsoup - 为什么无法抓取此网站?

如何使用 Beautifulsoup4

使用 Python 抓取 HTML 中的特定元素:BeautifulSoup4