![](/img/trans.png)
[英]Why am I receiving a name error for a name not defined that I haven't included in my code?
[英]Connection error for requests and BeautifulSoup4. Last night my code worked and I haven't changed anything
我正在編寫執行 web 抓取的代碼。 我試圖從劍橋詞典網站獲取 HTML 代碼,但彈出一條錯誤消息。 如果您能告訴我錯誤的原因和解決此問題的方法,我將不勝感激。
這是我的代碼:
import requests
from bs4 import BeautifulSoup
def checkWord(word):
url_top = "https://dictionary.cambridge.org/dictionary/english/"
url = url_top + word
headers = requests.utils.default_headers()
headers.update(
{
'User-Agent': 'My User Agent 1.0',
}
)
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
check = soup.find("title")
boolean = check.string
if boolean == "Cambridge English Dictionary: Meanings & Definitions":
return False
else:
return True
word = "App"
checkWord(word)
但是,錯誤發生在html = requests.get(url, headers=headers).text
錯誤信息如下所示——
Exception has occurred: ConnectionError
('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
During handling of the above exception, another exception occurred:
File "<string>", line 3, in raise_from
During handling of the above exception, another exception occurred:
File "<string>", line 3, in raise_from
During handling of the above exception, another exception occurred:
您的代碼一直運行良好。 最有可能的問題是你的本地 inte.net 這就是為什么它可能是暫時的或檢查你的 inte.net 連接
import requests
from bs4 import BeautifulSoup
def checkWord(word):
url_top = "https://dictionary.cambridge.org/dictionary/english/"
url = url_top + word
headers = requests.utils.default_headers()
headers.update(
{
'User-Agent': 'Mozilla/5.0',
}
)
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
check = soup.find("title").text
print(check)
word = "App"
checkWord(word)
Output:
APP | meaning, definition in Cambridge English Dictionary
看起來遠程主機禁止了您。 如果您仍然可以使用 web 瀏覽器從您的計算機打開該網站,請嘗試將用戶代理更改為如下所示:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.