Home > Enterprise >  Websrcaping html <time class=""></time>
Websrcaping html <time class=""></time>

Time:04-03

I would like to get the date. But I have got just a None.

<div >
<div >
<img src="https://api.hvg.hu/Img/da658e97-86c0-40f3-acd3-b0a850f32c30/e6f183bb-25a9-468e-ae30-6d98952ffc00.jpg" alt="Will Smith lemondott amerikai filmakadémiai tagságáról" width="800" height="370">
</div>
<div >
<div >
<div >

<time  datetime="2022-04-02T08:13:00.0000000 02:00">2022. április. 02. 08:13</time>

<time  datetime="2022-04-02T08:16:17.0000000 02:00">2022. április. 02. 08:16</time>
<a href="/kultura" >Kult</a>
</div>
</div>
<div >
<h1>Will Smith lemondott amerikai filmakadémiai tagságáról</h1>
</div>
</div>
<button  data-id="994e8ff1-f28e-4153-9f6c-87283f187af7" data-event-category="Myhvg_article_save" data-event-action="ClickOnLink" data-event-label="Article_save_MTI"></button>
</div>

I would like to get back: 2022. április. 02. 08:13

My code is:

article_soup = BeautifulSoup(article.content, "html.parser")
d=article_soup.find('time', class\_='article-datetime')

CodePudding user response:

soup = BeautifulSoup(html, "html.parser")
adate = soup.findAll("time", {"class": "article-datetime"})
print(adate[0].get_text())

CodePudding user response:

It's working.You have to remove \ from class

time = soup.find('time', class_='article-datetime').text
print(time)

Output:

2022. április. 02. 08:13

CodePudding user response:

Main issue is a typo class\_='article-datetime' and to get the text use simply the get_text() method:

article_soup.find('time', class_='article-datetime').get_text()

or

article_soup.find('time', class_='article-datetime').text

Example

html = '''
<div >
<div >
<img src="https://api.hvg.hu/Img/da658e97-86c0-40f3-acd3-b0a850f32c30/e6f183bb-25a9-468e-ae30-6d98952ffc00.jpg" alt="Will Smith lemondott amerikai filmakadémiai tagságáról" width="800" height="370">
</div>
<div >
<div >
<div >

<time  datetime="2022-04-02T08:13:00.0000000 02:00">2022. április. 02. 08:13</time>

<time  datetime="2022-04-02T08:16:17.0000000 02:00">2022. április. 02. 08:16</time>
<a href="/kultura" >Kult</a>
</div>
</div>
<div >
<h1>Will Smith lemondott amerikai filmakadémiai tagságáról</h1>
</div>
</div>
<button  data-id="994e8ff1-f28e-4153-9f6c-87283f187af7" data-event-category="Myhvg_article_save" data-event-action="ClickOnLink" data-event-label="Article_save_MTI"></button>
</div>'''

article_soup = BeautifulSoup(html)

article_soup.find('time', class_='article-datetime').get_text()

Output

2022. április. 02. 08:13
  • Related