如何从Beautiful Soup中的本文中提取日期部分?

时间:2018-06-18 17:33:28

标签: python beautifulsoup

文字是:

<div class="fi-mu-list today" data-matchesdate="20180619">

我想从中提取20180619

我试过了: 1)

mat = soup.select("div.fi-mu-list today > span.fi-mu-list__head__date") print(mat["data-matchesdate"])

输出:TypeError: list indices must be integers or slices, not str

2) 还尝试了print(mat)

输出:[]

1 个答案:

答案 0 :(得分:1)

您可以使用BeautifulSoup.find

from bs4 import BeautifulSoup as soup
s = '<div class="fi-mu-list today" data-matchesdate="20180619">'
result = soup(s, 'html.parser').find('div')['data-matchesdate']

输出:

'20180619'