gpt4 book ai didi

python - 使用 Python 和 Beautiful Soup 分割抓取的文本

转载 作者:行者123 更新时间:2023-12-01 08:23:55 26 4
gpt4 key购买 nike

我已经从 this website 中抓取了时间表。我得到的输出是:

"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字符,如“\u00bb”,因此通过使用非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 将具有相同的 key 。 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)

希望这会有所帮助!

关于python - 使用 Python 和 Beautiful Soup 分割抓取的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54425347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com