简体   繁体   English

Python requests 模块卡在 requests.get() 上并超时

[英]Python requests module gets stuck on requests.get() and gets timed out

I've been trying to web scrape from the following site: "https://www.india.ford.com/cars/aspire/"我一直在尝试从以下站点抓取 web:“https://www.india.ford.com/cars/aspire/”

import requests
from bs4 import BeautifulSoup
import csv

response = requests.get("https://www.india.ford.com/cars/aspire/", timeout=5)

if response.status_code!=200:
    print("error!")
else:
    print(response.status_code)

The execution gets stuck indefinitely.执行被无限期地卡住了。

On using timeout=5使用timeout=5

I get the following error:我收到以下错误:

在此处输入图像描述

I'm new to this so sorry if this is a noob question.如果这是一个菜鸟问题,我对此很抱歉。 Any help is highly appreciated: :P非常感谢任何帮助::P

Timeout need to use try except.超时需要使用try except。

This page needs to disguise the browser.这个页面需要伪装浏览器。

try:
    headers = {
        'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',
    }
    response = requests.get("https://www.india.ford.com/cars/aspire/", headers=headers, timeout=5)

    if response.status_code != 200:
        print("error!")
    else:
        print(response.status_code)
except requests.exceptions.Timeout as error:
    print('time out')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM