Answer the question
In order to leave comments, you need to log in
Why is the thead tag in the table when parsing the site, but not the tbody?
""" Hello, the code throws an error trs = soup.find('table').find('tbody').find_all('tr')
AttributeError: 'NoneType' object has no attribute 'find
' the body of the table tag does not contain the tbody tag , but if you write thead , it finds it, what is it connected with and how to get access to
the tbody ???? cryptocurrencies from table"""
import requests
from bs4 import BeautifulSoup
import csv
def get_html(url):
r = requests.get(url)
return r.text
def write_csv(data):
with open('cmc.csv', 'a') as f:
writer = csv.writer(f)
pass
def get_page_data(html):
soup = BeautifulSoup(html, 'lxml')
trs = soup.find('table' ).find('tbody').find_all('tr')
# trs = soup.find('table', id='currencies').find('tbody').find_all('tr')
# in original code it was as in the comment and the example was parsed based on https://coinmarketcap.com/
# at the time of writing the lesson in the html code of the page, the table tag had an id, now it is not there
# it still enters the table, but for some reason it is in it only the thead tag and its contents are visible, the tbody tag is not found,
print(len(trs))
print(trs)
def main():
url='https://coinmarketcap.com/ '
get_page_data(get_html(url))
if __name__ == '__main__':
main()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question