python - 在Python中解析HTML数据

标签 python html web-scraping python-3.6 get-request

假设我有以下网站:

https://www.atcc.org/Products/All/CRL-2528.aspx#culturemethod

当您访问此网站时,它会显示大量信息。 就我而言,我只想了解“培养条件”部分的温度。

当你向下滚动网页时,你会看到 名为“文化条件”的部分

Atmosphere: air, 95%; carbon dioxide (CO2), 5%
Temperature: 37°C

使用 requests 库,我能够获取页面的 HTML 代码。当我保存 HTML 并在其中搜索我的数据时,它位于底部

以这种形式

                                    Culture Conditions

                                </th>

    <td>



                                            <div><strong>Atmosphere: </strong>air, 95%; carbon dioxide (CO<sub>2</sub>), 5%</div><div><strong>Temperature: </strong>37&deg;C</div>

我不知道此后该做什么。我研究过使用 BeautifulSoup 来解析 HTML,但没有成功。

这是我迄今为止拥有的所有代码。

import requests

url='https://www.atcc.org/Products/All/CRL-2528.aspx#culturemethod'

page = requests.get(url)
textPage = str(page.text)

file = open('test2', 'w')
file.write(textPage)
file.close()

最佳答案

import requests
from bs4 import BeautifulSoup

url = 'https://www.atcc.org/Products/All/CRL-2528.aspx#culturemethod'

r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')

cc = soup.select('#layoutcontent_2_middlecontent_0_productdetailcontent_0_maincontent_2_rptTabContent_rptFields_2_fieldRow_3 td div')

for c in cc:
    print(c.text.strip())

输出:

Atmosphere: air, 95%; carbon dioxide (CO2), 5%
Temperature: 37°C

要获取温度:

cc = soup.select('#layoutcontent_2_middlecontent_0_productdetailcontent_0_maincontent_2_rptTabContent_rptFields_2_fieldRow_3 td div')[-1]
cc = cc.text.split(':')[-1].strip()
print(cc)

输出:

37°C

关于python - 在Python中解析HTML数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52525989/

相关文章:

html - 摆脱 Gmail 中的 "Show Quoted Text"

python - AttributeError : 'unicode' object has no attribute 'fromstring' . 如何解决这个问题?

python - 将具有多个数据的制表符分隔的文本文件加载到python中的数组中

python - 有没有办法在 SQLAlchemy 连接字符串中指定 Postgres 模式?

python - 在 mysql 中存储 python timedelta 值的最佳方法是什么?

html - 在 Google Drive 上显示 HTML 文件?

python - 将 Flask 应用程序部署到 Heroku

javascript - MeteorJS 外部文件 : css and js

python - 我应该如何正确使用Selenium

r - 在单词后获取文本--R 网页抓取