在python中是否有 in f string的替代方法?



所以我刮这个网站链接:https://www.americanexpress.com/in/credit-cards/payback-card/用漂亮的汤和蟒蛇。

link = 'https://www.americanexpress.com/in/credit-cards/payback-card/'
html = urlopen(link)
soup = BeautifulSoup(html, 'lxml')
details = []
for span in soup.select(".why-amex__subtitle span"):
details.append(f'{span.get_text(strip=True)}: {span.find_next("span").get_text(strip=True)}')
print(details)

输出:

['EARN POINTS: Earn multiple Points from more than 50 PAYBACK partners2and 2 PAYBACK Points from Americanxa0Express PAYBACK Creditxa0Card for every Rs.xa0100 spent', 'WELCOME GIFT: Get Flipkart voucher worth Rs. 7503on taking 3 transactions within 60 days of Cardmembership', 'MILESTONE BENEFITS: Flipkart vouchers4worth Rs. 7,000 on spending Rs. 2.5 lacs in a Cardmembership yearYou will earn a Flipkart voucher4worth Rs. 2,000 on spending Rs. 1.25 lacs in a Cardmembership year. Additionally, you will earn a Flipkart voucher4worth Rs. 5,000 on spending Rs. 2.5 lacs in a Cardmembership year.']
在输出中可以看到xa0要从字符串中消除的字符。

我试图使用替换函数,但它不工作与f字符串,因为有涉及。

details.append(f'{span.get_text(strip=True)}: {span.find_next("span").get_text(strip=True).replace("xa0","")}')

还有别的办法吗?

任何帮助都是非常感谢的!

可以使用unicodedata来删除xa0字符。当包含在f字符串中时,它将不会运行,但是可以这样做:

from bs4 import BeautifulSoup
from unicodedata import normalize
link = 'https://www.americanexpress.com/in/credit-cards/payback-card/'
html = urlopen(link)
soup = BeautifulSoup(html, 'lxml')
details = []
for span in soup.select(".why-amex__subtitle span"):
a = normalize('NFKD', span.get_text(strip=True))
b = normalize('NFKD',span.find_next("span").get_text(strip=True))
details.append(f'{a}: {b}')
print(details)

这可以是一个临时的解决方案,因为.replace("xa0","")不工作在外面做更改之前:

link = 'https://www.americanexpress.com/in/credit-cards/payback-card/'
html = urlopen(link)
soup = BeautifulSoup(html, 'lxml')
details = []
for span in soup.select(".why-amex__subtitle span"):
element = span.get_text(strip=True).replace("xa0","")
next_element = span.find_next("span").get_text(strip=True).replace("xa0","")
details.append(f'{element}: {next_element}')
print(details)

最新更新