![](/img/trans.png)
[英]Scraped Span Returns None Get_Text() Python Beautiful Soup
[英]Dividing scraped text with Python and Beautiful Soup
我已經從這個網站上抓取了時間表。 我得到的輸出是:
"ROUTE": "NAPOLI PORTA DI MASSA \u00bb ISCHIA"
但我想:
"DEPARTURE PORT": "NAPOLI PORTA DI MASSA"
"ARRIVAL PORT": "ISCHIA"
我如何分割字符串? 這是代碼:
medmar_live_departures_table = list(soup.select('li.tratta'))
departure_time = []
for li in medmar_live_departures_table:
next_li = li.find_next_sibling("li")
while next_li and next_li.get("data-toggle"):
if next_li.get("class") == ["corsa-yes"]:
# departure_time.append(next_li.strong.text)
medmar_live_departures_data.append({
'ROUTE' : li.text
})
兩件事情,
1.由於“»”是一個非ascii字符python正在返回像“\»”這樣的非ascii字符,因此通過用非ascii代碼分割文本來解析字符串,就像這樣可以工作:
parse=li.get_text().split('\u00bb')
此外,您可以使用 re 庫來解析非 ascii 字符(如果選擇此路徑,則需要添加 re 庫):
import re
non_ascii = li.get_text()
parse = re.split('[^\x00-\x7f]', non_ascii)
#[^\x00-\x7f] will select non-ascii characters as pointed out by Moinuddin Quadri in https://stackoverflow.com/questions/40872126/python-replace-non-ascii-character-in-string
但是,通過這樣做,python 將從解析中創建一個部分列表,但並非“li”html 標簽中的所有文本都帶有“»”字符(即在表格末尾的文本“POZZUOLI-PROCIDA”)網站)所以我們必須考慮到這一點,否則我們會遇到一些問題。
2.字典可能是一個糟糕的數據結構選擇,因為您正在解析的數據將具有相同的鍵。
例如, POUZZOULI » CASAMICCIOLA 和 POUZOULI » PROCIDA。 COSMICCIOLA 和 PROCIDA 將具有相同的密鑰。 Python 將簡單地覆蓋/更新 POUZZOULI 鍵的值。 所以 POUZZOULI: CASAMICCIOLA 將變成 POUZZOULI: PROCIDA 而不是將 POUZZOULI: CASAMICCIOLA 添加為字典條目,將 POUZZOULI: PROCIDA 添加為另一個字典條目。
我建議將解析的每個部分作為元組添加到列表中,如下所示:
single_port= []
ports=[]
medmar_live_departures_table = list(bs.select('li.tratta'))
departure_time = []
for li in medmar_live_departures_table:
next_li = li.find_next_sibling("li")
while next_li and next_li.get("data-toggle"):
if next_li.get("class") == ["corsa-yes"]:
# departure_time.append(next_li.strong.text)
non_ascii = li.get_text()
parse = re.split('[^\x00-\x7f]', non_ascii)
# The if statement takes care of table data strings that don't have the non-ascii character "»"
if len(parse) > 1:
ports.append((parse[0], parse[1]))
else:
single_port.append(parse[0])
# This will print out your data in your desired manner
for i in ports:
print("DEPARTURE: "+i[0])
print("ARRIVAL: "+i[1])
for i in single_port:
print(i)
我還在運行的測試代碼中使用了 split 方法:
import requests
from bs4 import BeautifulSoup
import re
url="https://www.medmargroup.it/"
response=requests.get(url)
bs=BeautifulSoup(response.text, 'html.parser')
timeTable=bs.find('section', class_="primarystyle-timetable")
medmar_live_departures_table=timeTable.find('ul')
single_port= []
ports=[]
for li in medmar_live_departures_table.find_all('li', class_="tratta"):
parse=li.get_text().split('\u00bb')
if len(parse)>1:
ports.append((parse[0],parse[1]))
else:
single_port.append(parse[0])
for i in ports:
print("DEPARTURE: "+i[0])
print("ARRIVAL: "+i[1])
for i in single_port:
print(i)
我希望這有幫助!
嘗試這個:
medmar_live_departures_table = list(soup.select('li.tratta'))
departure_time = []
for li in medmar_live_departures_table:
next_li = li.find_next_sibling("li")
while next_li and next_li.get("data-toggle"):
if next_li.get("class") == ["corsa-yes"]:
# departure_time.append(next_li.strong.text)
medmar_live_departures_data.append({
'DEPARTURE PORT' : li.text.split("\ u00bb")[0],
'ARRIVAL PORT' : li.text.split("\ u00bb")[1]
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.