编写了一个脚本来获取http响应。但是如果有一个不存在的url,脚本就会中断

2024-05-18 08:35:21 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是获取http响应的代码:

import requests
import xlrd
import xlwt

file_location = "C:/Users/Nitin Kansal/Desktop/rc/Cashkaro.xlsx"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)

urls = []
for r in range(sheet.nrows):
    urls.append(sheet.cell_value(r,0))

book = xlwt.Workbook(encoding = "utf-8", style_compression = 0)
sheet = book.add_sheet("Sheet1", cell_overwrite_ok = True)

for index, url in enumerate(urls):
    r_response = requests.head(url)

    try:
        Response_Number = r_response.status_code
        sheet.write(index, 0, url)
        sheet.write(index, 1, Response_Number)

    except Exception:
        sheet.write(index, 0, url)
        sheet.write(index, 1, "Failed to Fetch Response")

book.save("Response Output.xls")

下面是网址,保存到excel中,我添加了一个不存在的网址“nitinkansal121.com”。这打破了密码。如果这个网址被删除,他们的代码运行良好

https://clk.omgt5.com/?AID=183476&PID=9319&WID=33968
http://clk.omgt5.com/?AID=183476&PID=9166&WID=33968
http://clk.omgt5.com/?AID=183476&PID=14173&WID=33968
http://www.nitinkansal121.com/
http://clk.omgt5.com/?AID=183476&PID=9394&WID=33968

Tags: importcomhttpurlindexresponsepidurls
1条回答
网友
1楼 · 发布于 2024-05-18 08:35:21

requests.head如果在不存在的域上传递URL,则会引发异常

您可以替换:

r_response = requsts.head(url)

比如:

try:
    r_response = requests.head(url)
except requests.exceptions.ConnectionError:
    sheet.write(index, 0 , url)
    sheet.write(index, 1, "Connection error")

相关问题 更多 >