请求无法获取页面

时间:2020-06-30 19:02:29

标签: python beautifulsoup python-requests user-agent

我正在尝试使用“美丽汤”来检索this page

这是我尝试过的代码:

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.nasdaq.com/market-activity/stocks/msft/news-headlines")

每次我运行代码时,它都会卡住并且无法检索该页面。但是,我曾经收到一次ReadTimeout异常(requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='www.nasdaq.com', port=443): Read timed out. (read timeout=None))。

对于此问题的任何帮助或修复,我们将不胜感激。

2 个答案:

答案 0 :(得分:1)

我在请求中包含标头,它似乎可以正常工作。我使用了与浏览器发送的标题相同的标题,您可以使用开发者工具(如indicated here)找到它们。

import requests

headers = {
    "authority": "www.nasdaq.com",
    "method": "GET",
    "path": "/market-activity/stocks/msft/news-headlines",
    "scheme": "https",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-CA,en;q=0.9,ro-RO;q=0.8,ro;q=0.7,en-GB;q=0.6,en-US;q=0.5",
    "cache-control": "max-age=0",
    "dnt": "1",
    "if-modified-since": "Tue, 30 Jun 2020 19:43:05 GMT",
    "if-none-match": "1593546185",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "none",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
}

page = requests.get("https://www.nasdaq.com/market-activity/stocks/msft/news-headlines", headers=headers)

答案 1 :(得分:0)

而不是这样做

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.nasdaq.com/market-activity/stocks/msft/news-headlines")

尝试以这种方式检索网页:

from urllib.request import Request, urlopen
from bs4 import BeautifulSoup 

page = Request("https://www.nasdaq.com/market-activity/stocks/msft/news-headlines")