Python 如何替换输出中的字符串

Python 如何替换输出中的字符串,python,Python,我想用一些空白字符替换输出中的第一个字符串,只留下第二个字符串 r = s.get("https://www.urbanjunglestore.com/it/air-force-1-lv8-3-gs-42366.html") soup = BeautifulSoup(r.text,"html.parser") token = str(soup.find_all('script')[0]) token = str(token).replace('<script src="https://w

我想用一些空白字符替换输出中的第一个字符串,只留下第二个字符串


r = s.get("https://www.urbanjunglestore.com/it/air-force-1-lv8-3-gs-42366.html")
soup = BeautifulSoup(r.text,"html.parser")
token = str(soup.find_all('script')[0])
token = str(token).replace('<script src="https://www.urbanjunglestore.com/js/prototype/prototype.js"', '').replace('></script>', '').strip()
#print(token)
js = str(soup.find_all('script')[73]).replace("</script>", '')
js = str(js).replace('<script type="' , '').replace('-text/javascript">' , '')

print(js)
基本上,我想做的是用一些空格替换字符串'ee163bcc3ec44d33fbcc5647e'。 谢谢所有帮助我的人

您应该使用.text或.get\u text来代替str

然后你得到的脚本没有标签,你不需要替换任何字符串


编辑:坦白地说,我不知道你是如何得到ee163bcc3ec44d33fbc5647e的-我在运行你的代码时没有得到这个字符串。也许你是从其他印刷品上得到的?所以你应该去掉其他的印刷品。

这种东西闻起来像是一种味道。我可以看到你在做一些网站垃圾-你能分享一下你想从网页上刮取什么,以及你期望的确切输出吗?@user10987432我试图只打印var SpConfig,不包括前面的字符串。有没有办法取消它?它是一个字符串,所以你可以像js[20:]一样分割它,但你必须使用正确的值,而不是20-你甚至可以手动计算它。如果它是在两行中,那么在lines-lines=split\n-中拆分,然后得到第二行-lines[1]。如果在多行中,则在行中拆分,并重新连接除第一行之外的所有行-js=\n.joinlines[1:],而不是在strsoup中使用str。find_all'script'[73]应该使用.text或.get_text-js=soup.find_all'script'[73]。text,然后不必替换任何文本。
ee163bcc3ec44d33fbc5647e
        var spConfig = new Product.Config({"attributes"..
 js = soup.find_all('script')[73].text
import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.urbanjunglestore.com/it/air-force-1-lv8-3-gs-42366.html")
soup = BeautifulSoup(r.text,"html.parser")
js = soup.find_all('script')[73].text

print(js)