我正在嘗試抓取一個網站以列印出事件的時間和日期
with open('events.html', 'r', encoding='utf-8') as html_file:
content = html_file.read()
soup = BeautifulSoup(content, 'lxml')
free_slot = soup.find_all('tr', class_='views-field views-field-title')
for slot in free_slot:
event_name = slot.a.text
event_time = slot.time.text
print(event_name)
print(event_time)
events.html 包含這個
旁觀者干預:現場研討會 Glasnevin Campus Solas Room,The U Student Support & Development 2 月 15 日 13:00 - 2 月 15 日 13:50html 來自這個網站:https ://www.dcu.ie/students/events 當我嘗試運行代碼時它只回傳'[]'
uj5u.com熱心網友回復:
怎么了?
ResultSet 是空的<tr>,因為您的find_all().
怎么修?
從你的類中洗掉類find_all()并迭代:
free_slot = soup.find_all('tr')
for slot in free_slot:
print(slot)
event_name = slot.a.text
event_time = slot.time.text
print(event_name)
print(event_time)
怎么刮桌子?
您可以使用它來完成它,但我認為要獲取 table 的內容,使用built-inBeautifulSoup更簡單,這將為您完成這項作業:pandasread_html
import pandas as pd
pd.read_html('https://www.dcu.ie/students/events')[0]
輸出
| 未命名:0 | 校園 | 場地 | 部 | 活動日期 |
|---|---|---|---|---|
| 旁觀者干預:現場研討會 | 格拉斯內文校區 | 索拉斯室,U | 學生支持與發展 | 2月15日 13:00 - 2月15日 13:50 |
| 情商:緩解壓力和焦慮的方法 - 第 2 節 | 在線的 | 通過縮放在線 | 學生支持與發展 | 2月15日 13:00 - 2月15日 14:00 |
| 批判性寫作 | 在線的 | 通過縮放在線 | 學生學習 | 2月15日 13:00 - 2月15日 14:00 |
| 技能會議:提高你的面試技巧 | 在線的 | 在線的 | 職業服務 | 2月15日 13:00 - 2月15日 13:50 |
| 旁觀者干預:現場研討會 | 圣帕特里克校園 | B108,禮堂 | 學生支持與發展 | 2月15日 17:00 - 2月15日 17:50 |
| 旁觀者干預:現場研討會 | 格拉斯內文校區 | 翠林廳,The U | 學生支持與發展 | 2月15日 18:00 - 2月15日 18:50 |
| 如何通過微軟的技術面試 | 在線的 | 在線的 | 職業服務 | 2月16日 10:00 - 2月16日 11:00 |
| 走向全球求職培訓課程 | 在線的 | 虛擬的 | 職業服務 | 2月16日 10:00 - 2月16日 11:00 |
| 關于沃達豐愛爾蘭暑期實習計劃 2022 的資訊化會議和問答。 | 在線的 | 在線的 | 職業服務 | 2月16日 12:00 - 2月16日 13:00 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/424254.html
