Reputation: 301
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)
Upvotes: 1
Views: 95
Reputation: 195448
.contents
will select all content inside a tag - so strings, HTML comments, tags etc. Instead of .contents
use .find_all
/.select
to select tags of interest, for example:
import requests
from bs4 import BeautifulSoup
url = "https://icc-ccs.org/index.php/piracy-reporting-centre/live-piracy-report"
results = requests.get(url).text
doc = BeautifulSoup(results, "html.parser")
tbody = doc.tbody
trs = tbody.select("tr") # <-- select all <tr>
for tr in trs:
for td in tr.select("td"): # <-- select all <td> inside <tr>
print(td.get_text(strip=True), end=" ")
print()
Prints:
No records
041-22 13.04.2022: 0345 UTC: Posn: 04:53.9N – 001:40.9W, Takoradi Anchorage, Ghana.Two robbers in a fishing boat approached and boarded an anchored bulk carrier during heavy rain. They broke into the forward store and stole ship’s stores. The duty crew on rounds noticed the robbers, and informed the officer on watch who raised the general alarm and sounded the ship’s horn. Crew mustered. Hearing the alerted crew the robbers escaped. Port control informed. A patrol boat was sent to the location. 2022-04-13 View
040-22 09.04.2022: 0800 UTC: Posn: 22:13.5N – 091:44.1E, Chittagong Anchorage, Bangladesh.Unnoticed, robbers boarded an anchored bulk carrier during cargo operations. They stole the rescue boat's engine and escaped. The theft was noticed by the duty crew on rounds. A search was carried out. Authorities informed. 2022-04-09 View
039-22 03.04.2022: 1848 UTC: Posn: 01:31.20N – 001:30.68E, Around 261nm SE of Accra, Ghana.A bulk carrier was boarded by an unknown number of pirates and the crew retreated into the citadel. On being notified of the incident, the IMB Piracy Reporting Centre immediately alerted and liaised with the Regional Authorities and international warships to request their assistance. An Italian Navy warship and its helicopter responded and proceeded to render assistance resulting in the crew and ship being safe. The vessel proceeded to a safe port under the escort of the warship. 2022-04-03 View
038-22 02.04.2022: 1630 UTC: Posn: 13:45.4N – 121:01.6E, Batangas Anchorage, Philippines.Five robbers boarded an anchored vehicle carrier via the secured hawse pipe through a small gap between the cover and the anchor chain. Duty crew noticed the robbers and retreated to the bridge. Alarm raised and crew mustered. Seeing the crew alertness, the robbers escaped with stolen ship’s properties. Incident reported to VTS and the coast guard boarded the ship for inspections. 2022-04-02 View
037-22 23.03.2022: 2120 UTC: Posn: 03:55N - 098:46E, Belawan Anchorage, Indonesia.Three robbers armed with knives boarded an anchored bulk carrier. Duty crew on routine rounds noticed the robbers near the forecastle and raised the alarm. Crew mustered. Seeing the crew alertness, the robbers escaped with stolen ship’s properties. The incident was reported to port authorities. 2022-03-23 View
036-22 27.02.2022: 1800 UTC: Posn: 01:02.82N – 103:38.11E, Singapore Straits.Duty Engineer onboard a bulk carrier underway was alerted by the trainee engineer of the presence of robbers in the workshop. He informed the OOW who raised the alarm and made an announcement on the PA resulting in all crew mustering. On hearing the alarm, the robbers tried to escape but were confronted by the oiler and 4th Engineer. The robbers waved their knives at them, and escaped. A search was carried out. It was reported that ship’s spares were stolen. VTS informed. 2022-02-27 View
035-22 15.03.2022: 1655 UTC: Posn: 22:14.3N – 091:49.6E, CUFL Jetty, Chittagong, Bangladesh.Six robbers boarded a berthed chemical tanker during cargo operations. Duty crew noticed the robbers near the forecastle store. Alarm raised and crew mustered. Hearing the alarm and seeing the crew alertness, the robbers escaped with stolen ship's stores. Port control and coast guard informed and inspections carried out. Additional watchmen were arranged by the local agent. Later, the coast guard recovered the stolen items and returned it to the tanker. 2022-03-15 View
034-22 14.03.2022: 0100 UTC: Posn: 12:01.61S - 077:11.83W, Callao Anchorage, Peru.Seven robbers armed with knives boarded an anchored container vessel. Alarm raised, PA announcement made, and crew mustered resulting in the robbers escaping with stolen ship's properties and stores. Incident reported to TRAMAR and the port authorities boarded the ship for investigation. 2022-03-14 View
033-22 13.03.2022: 0100 UTC: Posn: 12:01.0S – 077:12.0W, Callao Anchorage, Peru.Unnoticed, robbers boarded an anchored vehicle carrier and escaped with ship’s properties. The theft was noticed by the duty crew on routine rounds. Incident reported to port authorities. 2022-03-13 View
032-22 10.03.2022: 0800 UTC: Posn: 12:00.0S – 077:12.7W, Callao Anchorage, Peru.Around seven robbers armed with long knives in a small boat approached and boarded an anchored bulk carrier during dense fog. They took hostage the duty AB and stole ship’s stores. The bridge duty officer tried to contact the AB. When no response was received he instructed the duty bosun and OS to investigate. Alarm raised and crew mustered. Hearing the alarm and seeing the crew's alertness, the robbers released the duty AB and escaped in their boat. The incident was reported to the port authorities, local agent, and CSO. 2022-03-10 View
Upvotes: 2