我试着从下面的网站上摘录媒体的声明和讲话。
我的问题与这个问题非常相似。Finding the correct elements for scraping a website
from bs4 import BeautifulSoup
from selenium import webdriver
base_url = 'https://www.ecb.europa.eu'
urls = [
f'{base_url}/press/pr/html/index.en.html',
f'{base_url}/press/govcdec/html/index.en.html'
]
driver = webdriver.Chrome()
for url in urls:
driver.get(url)
soup = BeautifulSoup(driver.page_source, 'html.parser')
for anchor in soup.select('span.doc-title > a[href]'):
driver.get(f'{base_url}{anchor["href"]}')
article_soup = BeautifulSoup(driver.page_source, 'html.parser')
title = article_soup.select_one('h1.ecb-pressContentTitle').text
date = article_soup.select_one('p.ecb-publicationDate').text
paragraphs = article_soup.select('div.ecb-pressContent > article > p:not([class])')
content = '\n\n'.join(p.text for p in paragraphs)
print(f'title: {title}')
print(f'date: {date}')
print(f'content: {content[0:80]}...')
但是,我已经尝试运行它,但没有得到任何输出。我在HTML方面的经验很少。特别是,我不明白这是什么部分在循环。与CSS相关的东西。
for anchor in soup.select('span.doc-title > a[href]'):
因此,我怀疑它不再起作用了,因为最近欧洲央行网页的布局发生了变化。我猜html引用发生了变化,但我不知道确切的情况
非常感谢你的帮助。
发布于 2021-03-05 12:22:19
我可以知道你到底需要什么答案吗?可以使用.find_all()查找特定标签、类或id的所有元素。Tag:在字符串中键入标记的名称类: Type class_="TheNameOfClass“Id: Type id="NameOFID”
希望这能帮上忙。如果你对此有疑问,一定要问其他任何问题。
发布于 2021-03-08 19:21:04
我需要以下内容
<div class="title"> <h1>Unconventional fiscal and monetary (...) </h1>
<h2 class="ecb-pressContentSubtitle">Keynote speech by Isabel Schnabel, (...)”</h2>
<p class="ecb-publicationDate">Frankfurt am Main, 26 February 2021</p>
<p>One of the greatest conundrums (...)
因此,我的输出将如下所示
标题:非常规财政和货币政策(...)副标题:伊莎贝尔·施纳贝尔的主题演讲,(...)
日期:2019年12月20日
内容:最大的难题之一(...)
发布于 2021-03-09 16:44:24
哦,好的,首先你会在driver = webdriver.Chrome()
中得到一个错误。在括号内,您应该粘贴安装的webdriver的确切路径。如果您没有适用于chrome的webdriver,请按照以下说明进行操作。现在,如果你已经在那里指定了webdriver路径,并且不想把它泄露给其他人,那就没问题了。
接下来,您可以使用selenium本身来抓取元素,而不是使用漂亮的汤。使用driver.find_element_by_class_name("")
并键入要搜索的类名。在这种情况下,它将是"title“。接下来,您想要获取h1的文本。我不知道如何在selenium中找到孩子,你可以搜索一下,然后使用命令获得h1的代码。将其存储在一个变量中,您可以像这样打印它的文本print(h1.text)
。注意:搜索如何从selenium获取标记的子元素,然后使用任何网站上提到的命令,然后使用.text
使用说明:
webdriver.Chrome()
.中
这就是全部。如果我的回答不能让你满意,请一定要问,因为我不是selenium的专家。我也是个学徒。
https://stackoverflow.com/questions/66492436
复制相似问题