【问题标题】:how to get data inside div element that doesnt have an ID using soup object如何使用汤对象获取没有ID的div元素内的数据
【发布时间】:2020-04-03 20:06:54
【问题描述】:

我正在使用 BeautifulSoup 并在一个网站上工作

"https://www.worldometers.info/coronavirus/",

我有一个没有 ID 的 div 元素,我想获取 div 中的内容并将其存储在变量中

Eg: "Last updated: April 03, 2020, 03:34 GMT"

这是 div 元素:

<div style="font-size:13px; color:#999; text-align:center">Last updated: April 03, 2020, 03:34 GMT</div>

有人可以帮我解决这个问题吗?提前致谢

【问题讨论】:

  • 最后更新时间:2020 年 4 月 3 日,格林威治标准时间 03:34

标签: html python-3.x web-scraping beautifulsoup


【解决方案1】:

使用regular expression re并搜索带有文本Last updated的div

import requests
from bs4 import BeautifulSoup
import re
r=requests.get("https://www.worldometers.info/coronavirus/").text
soup=BeautifulSoup(r,'html.parser')
strlastdate=soup.find('div',text=re.compile("Last updated")).text
print(strlastdate)

输出

Last updated: April 03, 2020, 20:35 GMT

【讨论】:

  • 这很好用。非常感谢您的回复。每天都在学习新事物……而且你很棒
猜你喜欢
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
相关资源
最近更新 更多