This is my first time using BeautifulSoup and I am trying to parse a table at https://icc-ccs.org/index.php/piracy-reporting-centre/live-piracy-report. After trying to work this out for almost 24 hours I have resorted to coming here to ask
To help me get this working I have been working through a youtube tutorial and I get to a part where I need to add '.contents. property to my iterator but if I do that I get an "AttributeError: 'NavigableString' object has no attribute 'contents'" error.
Using it with the tutorial URL it works fine - https://coinmarketcap.com
The error occurs in this snippet of code
for tr in trs:
for td in tr.contents:
print(td)
The full code is as follows. As I said if I swap out the URLs it works perfectly. I have read around and can't figure out the AttributeError. I put that down to my newness with BeautifulSoup.
Here is my full code
import requests
from bs4 import BeautifulSoup
# Our target URL
url = 'https://icc-ccs.org/index.php/piracy-reporting-centre/live-piracy-report'
#url = "https://coinmarketcap.com"
# Grab our data as text
# type is: <class 'str'>
results = requests.get(url).text
# Stick it into a Beautiful Soup object
# type is: <class 'bs4.BeautifulSoup'>
doc = BeautifulSoup(results, 'html.parser')
# Get the tbody section we are interested in
# type is: <class 'bs4.element.Tag'>
tbody = doc.tbody
# Provides a list of all the tags inside tbody
# type is: <class 'list'>
trs = tbody.contents
# Prints out all the table contents we are interested in
# from item 3 through item 21 stepping 2 at a time
# 3, 5, 7, 9, 11, 13, 15, 17, 19, 21 a total of 10 table items
#print(trs[3:22:2])
for tr in trs:
# Errors here with attribute Error if using ICC URL
# Works fine with coinmarketcap.com
for td in tr.contents:
print(td)