英文:
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'))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论