user13651239
user13651239

Reputation:

beautifulsoup get_text() function not working

so, i was making a covid map using web scraping, but i have never used it, anyways here is my python code

    import requests
from bs4 import BeautifulSoup

page = requests.get('https://news.google.com/covid19/map?hl=en-US&gl=US&ceid=US%3Aen&mid=%2Fm%2F016zwt')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.find_all(class_='l3HOY')
print(week[7]).get_text()

but when i try to run it, it gives this error

  print(week[7].find('td').get_text())
AttributeError: 'NoneType' object has no attribute 'get_text'

can anyone tell me how i can fix it?

Upvotes: 0

Views: 989

Answers (1)

bigbounty
bigbounty

Reputation: 17408

import requests
from bs4 import BeautifulSoup

page = requests.get('https://news.google.com/covid19/map?hl=en-US&gl=US&ceid=US%3Aen&mid=%2Fm%2F016zwt')
soup = BeautifulSoup(page.content, 'html.parser')
week = soup.select(".l3HOY")
print(week[7].get_text())

Output: 15,964

Changes you have to make

  1. get_text method is on the week element not on the print statement
  2. If you want all the elements with a particular class, then better go with css selector

Upvotes: 1

Related Questions