Search code examples
pythondatetimeweb-scrapingstrptime

I am trying to solve it but not solved, "time data ' Sept 26 2022' does not match format ' %b %d %Y'"


Here is the article link and code. https://www.nbcnews.com/business/travel/biden-administration-propose-new-rules-airline-fees-requiring-transpar-rcna49416

published_Date = soup.find('time', class_="relative z-1").get_text(strip=True).split(", ")
Published_Date = ""
published_Date = published_Date[0:2]
for i in published_Date:
    Published_Date += " " + i
Published_Date = Published_Date.replace(',',"").replace('.',"")
datetime_obj = datetime.strptime(Published_Date, ' %b %d %Y')
Published_Date = datetime_obj.`enter code here`date()
print("\nPublished Date: ", Published_Date )

Solution

  • You can get that date with:

    published_Date = soup.select_one('time[class="relative z-1"]').get('datetime').split('T')[0]
    

    Documentation for BeautifulSoup is quite good: https://beautiful-soup-4.readthedocs.io/en/latest/