如何用BeautifulSoup替换字母

huangapple go评论65阅读模式
英文:

How to replace a letter with BeautifulSoup

问题

I have this code in Python. It is running good. The problem is with two last href's, they have "ä" in it so it is not a link anymore. How can I replace "ä" with "%c3%a4" so it will open the right website?

import requests
from bs4 import BeautifulSoup

url = "https://www.gelbeseiten.de/"
req = requests.get(url)
src = req.text
soup = BeautifulSoup(src, "lxml")

all_categories = soup.find_all("a", class_="gc-link gc-link--blue")
for i in all_categories:
    print("https://www.gelbeseiten.de" + i.get("href").replace("ä", "%c3%a4"))
英文:

I have this code in Python. It is running good. The problem is with two last href's, they have "ä" in it so it is not a link anymore. How can I replace "ä" with "%c3%a4" so it will open the right website?

import requests
from bs4 import BeautifulSoup

url = "https://www.gelbeseiten.de/"
req = requests.get(url)
src = req.text
soup = BeautifulSoup(src, "lxml")

all_categories = soup.find_all("a", class_="gc-link gc-link--blue")
for i in all_categories:
    print("https://www.gelbeseiten.de" + i.get("href"))

答案1

得分: 1

尝试使用.replace方法替换它。

import requests
from bs4 import BeautifulSoup

url = "https://www.gelbeseiten.de/"
req = requests.get(url)
src = req.text
soup = BeautifulSoup(src, "lxml")

all_categories = soup.find_all("a", class_="gc-link gc-link--blue")
for i in all_categories:
    print("https://www.gelbeseiten.de" + i.get("href").replace('ä','%c3%a4'))
英文:

Try replacing it with .replace method.

import requests
from bs4 import BeautifulSoup

url = "https://www.gelbeseiten.de/"
req = requests.get(url)
src = req.text
soup = BeautifulSoup(src, "lxml")

all_categories = soup.find_all("a", class_="gc-link gc-link--blue")
for i in all_categories:
    print("https://www.gelbeseiten.de" + i.get("href").replace('ä','%c3%a4'))

huangapple
  • 本文由 发表于 2023年7月6日 16:39:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定