如何删除 ­从字符串? [英] How to remove ­ from string?

查看:37
本文介绍了如何删除 ­从字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 BeautifulSoup 进行刮擦.在网站上,他们在标题中使用 ­.

I am using BeautifulSoup to scrape. On a website they use ­ in the title.

原始html元素:<h1 itemprop="name">Pen&shy;ne met sa&shy;la&shy;mi en broc&shy;co&shy;li</h1>

当我刮它时,它返回:Penéne met sa la mi en broc co li"

When I scrape it, it returns this: "Pen�ne met sa�la�mi en broc�co�li"

每个 都是字符串中的 &shy;

Every � is an &shy; in the string

我试过 string.replace('\u00AD','')/string.replace('­',' ') 但没用

I've tried string.replace('\u00AD','') / string.replace('­­',' ') but it didn't work

import requests
from bs4 import BeautifulSoup
import mysql.connector

scrape_url = 'https://www.ah.nl/allerhande/recept/R-R377934/penne-met-salami-en-broccoli'
# get the data
data = requests.get(scrape_url)

#load the data into bs4
soup = BeautifulSoup(data.text, 'lxml')

titel = soup.find('h1').text
print(titel)

推荐答案

假设您使用的是 Python 3,我设法修复它如下:

Assuming you are using Python 3, I managed to fix it as follows:

s = '<h1 itemprop="name">Pen&shy;­ne met sa&shy;­la­&shy;­mi en broc&shy;­­co­&shy;­li</h1>'
s.replace("&shy", '').replace(";\xad","").replace("\xad","")

这给了我以下内容:

'<h1 itemprop="name">Penne met salami en broccoli</h1>'

这篇关于如何删除 &amp;shy;从字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆