Python 意外的输出结果
我遇到了意想不到的情况 我正在从网站上抓取文字 例如: 我搜索术语Python 意外的输出结果,python,beautifulsoup,Python,Beautifulsoup,我遇到了意想不到的情况 我正在从网站上抓取文字 例如: 我搜索术语niño,然后.py给出我想要的结果 (nee-nyoh) masculine or feminine noun 但是当我搜索que时,.py会给我一个完全错误的结果,如下所示 (kweh behk) 连词 根据,.py应该给我一个如下的结果 (keh) conjunction 所以我想知道这有什么问题? 有什么想法吗 谢谢 我已将我的代码包括在下面: import requests from bs4 import Beau
niño
,然后.py
给出我想要的结果
(nee-nyoh)
masculine or feminine noun
但是当我搜索que
时,.py
会给我一个完全错误的结果,如下所示
(kweh behk)
连词
根据,.py
应该给我一个如下的结果
(keh)
conjunction
所以我想知道这有什么问题?
有什么想法吗
谢谢
我已将我的代码包括在下面:
import requests
from bs4 import BeautifulSoup
base_url = "https://www.spanishdict.com/translate/"
search_keyword = input("input the keyword : ")
url = base_url + search_keyword
spanishdict_r = requests.get(url)
spanishdict_soup = BeautifulSoup(spanishdict_r.text, 'html.parser')
# Phonetic Alphabet
print(spanishdict_soup.find('span', {'class': 'dictionaryLink--369db'}).text)
# Part of Speech
print(spanishdict_soup.find('a', {'class': 'href--2RDqa'}).text)
# Meaning
只需将第一个搜索条件替换为:
spanishdict_soup.find("span", {"id": "dictionary-link-es"}).text
好的,可以在脚本标记中找到json格式的数据。这只是把它拔出来,然后抓住你需要的东西。除了在脚本标记内,我看不到从源html中获取它的位置:
import requests
import json
from bs4 import BeautifulSoup
base_url = "https://www.spanishdict.com/translate/"
search_keyword = input("input the keyword : ")
url = base_url + search_keyword
spanishdict_r = requests.get(url)
spanishdict_soup = BeautifulSoup(spanishdict_r.text, 'html.parser')
scripts = spanishdict_soup.find_all('script')
for script in scripts:
if 'SD_DICTIONARY_RESULTS_PROPS' in script.text:
jsonStr = script.text
jsonStr = jsonStr.split('global.SD_DICTIONARY_RESULTS_PROPS = ')[-1]
jsonStr = jsonStr.rsplit(';\n')[0]
jsonData = json.loads(jsonStr)
# Phonetic Alphabet
print(jsonData['es']['pronunciationDictionaryLink']['pronunciationSpellings'][0])
# Part of Speech
print(jsonData['es']['entry']['neodictFullQuickPartOfSpeechEn'])
# Meaning
输出:
input the keyword : que
keh
conjunction
当您想从西班牙语转换为英语时,您可以访问“从英语到西班牙语的html”选项卡。Nino和bot是一样的,而que是一样的different@chitown88我懂了。但我怎么才能让它只搜索西班牙语到英语的部分呢?我找不到这个的地址。是的,我正在调查。我可能有另一个解决方案给你。给我一分钟好的,找到了。给我一分钟左右的时间把代码写出来。Slobodan Ilic在矿下找到的溶液。接受他的回答。我的解决方案有效(只是一种不同的方法)。感谢您的帮助,但我遇到了一个错误。回溯(上次的最新调用):文件“spanishdict2.py”,第24行,打印(jsonData['es']['PronansionDictionaryLink']['PronansionSpellings'][0])keyrerror:'s'hmmm。这意味着返回的方式不同(没有'es'
键). 请问你是从美国和/或英国连接的吗?你可能还想考虑使用<代码> RAWIOPENTION/CODE >字符串(这样你就不必用双引号包装输入)。