【问题标题】:Beatiful Soup Extract InformationBeautifulsoup 提取信息
【发布时间】:2019-12-09 22:39:15
【问题描述】:

我正在尝试提取化学物质的名称、它的出现/用途以及通过使用美丽的汤添加的日期。 这是列表中化学品的一个示例 https://oehha.ca.gov/chemicals/abiraterone-acetate

有人可以帮我吗?非常感谢!

我的愿望输出将是

Abiraterone acetat from L253
<h1 class="title" id="page-title"><span class="ca-gov-icon-arrow-down"></span> Abiraterone acetate </h1>

A CYP17 inhibitor indicated in combination with prednisone for the treatment of patients with metastatic castration-resistant prostate cancer
from L265
<h3 class="label-above">Occurence(s)/Use(s)</h3><p>A CYP17 inhibitor indicated in combination with prednisone for the treatment of patients with metastatic castration-resistant prostate cancer.</p>

02/02/2016 from L266
<h3 class="label-above">Date Added</h3><span class="date-display-single" property="dc:date" datatype="xsd:dateTime" content="2016-02-02T00:00:00-08:00">02/02/2016</span>  </div>

【问题讨论】:

  • 您能否展示一下您尝试提取的化学物质名称?
  • temp = str(soup.find_all('p')) 我用它来查找化学物质的出现。但是事件没有出现
  • @RunyaoYin 被告知您正在处理Incapsula 防火墙,这将阻止任何尝试解析站点内容的尝试
  • @RunyaoYin 在下面查看我的答案

标签: python python-3.x beautifulsoup web-crawler


【解决方案1】:

请注意,该网站受到incapsula 防火墙的保护,以防止机器人和浏览器自动化。

使用Selenium,我们可以实现以下目标:

from selenium import webdriver
from bs4 import BeautifulSoup

browser = webdriver.Firefox()
url = 'https://oehha.ca.gov/chemicals/abiraterone-acetate'
sada = browser.get(url)
source = browser.page_source
soup = BeautifulSoup(source, 'html.parser')

title = soup.find('h1', {'class': 'title'})
print(title.text.strip())
details = soup.find(string='Occurence(s)/Use(s)').find_next('p').contents[0]
print(details)
date = soup.find('span', {'class': 'date-display-single'})
print(date.text)

browser.close()

输出:

Abiraterone acetate
A CYP17 inhibitor indicated in combination with prednisone for the treatment of patients with metastatic castration-resistant prostate cancer.
02/02/2016

【讨论】:

  • 您好,非常感谢您在这里的帮助。我试过你的代码,我收到一条错误消息:不是目录:'geckodriver'
  • @RunyaoYin 你需要安装驱动! click here
  • 您好,我下载了驱动后该怎么办。如何在 Mac 上执行?
  • @RunyaoYin 那么你已经下载Safari驱动了?正确的?所以把它放在/usr/bin/usr/local/bin
  • 根据您分享的网站,我实际上找不到下载 Safari 驱动程序的位置
猜你喜欢
  • 2019-04-15
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 2022-08-14
  • 2023-03-23
相关资源
最近更新 更多