我试图在我的刮刀中加载这个页面,但是每当我运行它时,终端永远不会完成运行。下一行永远不会出现,我甚至不能停止运行这行。如果我想继续,我必须关闭整个终端并启动一个新的终端。我不知道为什么会发生这种情况,考虑到相同的代码适用于我正在使用的大多数其他网站。有没有人知道为什么会发生这种情况,或者我怎么才能绕过它?
from urllib.request import urlopen as uReq
uWF = uReq('https://advisor.morganstanley.com/one-hundred-square-group', timeout=10)
page_html = uWF.read()
uWF.close()uReq行似乎是代码被卡住的部分。我在行中包含了"timeout=10“,这样我就不必不断地关闭和打开一个新的控制台。
发布于 2021-08-13 18:15:29
尝试使用requests模块,但设置User-Agent HTTP header:
import requests
headers = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0"
}
print(
requests.get(
"https://advisor.morganstanley.com/one-hundred-square-group",
headers=headers,
).text
)打印:
<!doctype html><html lang="en"><head>
...https://stackoverflow.com/questions/68766290
复制相似问题