这是我第一次使用BeautifulSoup,我试图在https://icc-ccs.org/index.php/piracy-reporting-centre/live-piracy-report上解析一个表。在试图解决这一问题将近24小时后,我不得不到这里来问
为了帮助我做到这一点,我一直在通过youtube的教程工作,我需要在其中添加“.contents”。属性设置为我的迭代器,但如果这样做,则会得到"AttributeError:'NavigableString‘对象没有属性'contents'“错误。
将它与教程URL一起使用,它可以很好地工作-- https://coinmarketcap.com。
此错误发生在此代码片段中。
for tr in trs:
for td in tr.contents:
print(td)完整代码如下所示。就像我说的,如果我交换URL,它就会工作得很好。我到处看过书,弄不懂AttributeError。我把这归因于我对BeautifulSoup的新鲜感。
这是我的完整代码
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)发布于 2022-04-17 22:49:49
.contents将选择标签中的所有内容-所以字符串、HTML注释、标记等。使用.find_all/.select代替.contents来选择感兴趣的标记,例如:
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()指纹:
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 https://stackoverflow.com/questions/71906011
复制相似问题