【问题标题】:Getting return codes of URLs python urllib获取 URL 的返回码 python urllib
【发布时间】:2018-12-10 19:38:57
【问题描述】:

我有一个 URL 列表,其中一些现在不起作用。我想解析该列表并获取这些 URL 的返回代码并将它们存储在数据框中。 我有以下代码:

for url in df['URL'][]:
print(url)
try:
    #print(urllib2.urlopen(url).getcode())
    df['returncode']=urllib2.urlopen(url).getcode()
except:
    df['returncode']='Obsolete'
    #print('obsolete')

我得到的是所有“过时”的列。

df['returncode']:
0         Obsolete
1         Obsolete
2         Obsolete
3         Obsolete
4         Obsolete
5         Obsolete
6         Obsolete
7         Obsolete
8         Obsolete
9         Obsolete
10        Obsolete
11        Obsolete

而如果我打印这些值,我可以看到不同的返回码。

http://study.com/odfv.html
obsolete
http://www.meghansfashion.com/uploads/2/1/2/9/21295692/2_75_orig.png
200
http://p16.muscdn.com/img/tos-maliva-p-0068/8ab65f6aac844cdf83526b5662720be3~c5_300x400.jpeg
200
http://config.88-f.net/hb/c1/pxbfwsp
obsolete

我在这里做错了什么?

【问题讨论】:

  • 永远不要使用裸露的except:,因为它会把明显的问题变成神秘的问题。如果您打算从urllib2 捕获异常,请考虑except urllib2.HTPPError:
  • 如果可能,你应该开始使用 Python v3.x - pythonclock.org
  • 我试过这个 - 对于 df['URL'][:10] 中的 url: response=requests.get(url) df['returncode']=response 但得到以下错误:- -------------------------------------------------- ------------------------------------ ConnectionError: HTTPConnectionPool(host='10.121.254.141', port=80): url: /moxa-cgi 超出最大重试次数/getsnapshot.cgi(由 NewConnectionError 引起(': 无法建立新连接:[Errno 61] Connection denied',))
  • 请编辑您的问题以使用新信息进行更新。

标签: python urllib


【解决方案1】:

您在 DataFrame 中输入结果的方式不起作用。命令

df['returncode']= ...

将值放在 DataFrame 的 每一行 中。所以你最后看到的是 last 找到的值的 11 倍。

要改善这一点,您需要将结果放入特定行。您可以像这样遍历行:

for index, row in df.iterrows():
    url = row['URL']
    print(url)
    try:
        #print(urllib2.urlopen(url).getcode())
        row['returncode']=urllib2.urlopen(url).getcode()
    except:
        row['returncode']='Obsolete'
        #print('obsolete')

【讨论】:

    【解决方案2】:

    获取 URL 的返回码 python urllib


    你可以使用requests来获取一个url上的http状态码,即:

    import requests
    response = requests.get("https://google.com")
    print (response.status_code)
    # 200
    

    【讨论】:

      【解决方案3】:

      您可以使用urllib2 获取http 响应代码。你是大多数 在那里,您只需要正确处理异常即可。 urllib2 收到错误的 http 响应时引发异常。

      import urllib2
      
      urls = ['http://www.google.com', 'http://google.com/does-not-exist']
      
      for url in urls:
          try:
              res = urllib2.urlopen(url)
              code = res.getcode()
          except urllib2.HTTPError as err:
              code = err.getcode()
      
          print('{}: {}'.format(url, code))
      

      这将输出:

      http://www.google.com: 200
      http://google.com/does-not-exist: 404
      

      【讨论】:

      • 我得到一些 URL 的 URLerror 和一些 HTTP 错误。 url 错误正在获取句柄,但 HTTPerror 在循环时给了我 BadStatusLine 错误。
      • 更新:对于这个“delivery-us-central-1.openx.net:443”,我得到了错误的状态行
      • 你得到一个BadStatusLine,因为你试图使用http获取一个https url。如果您将该网址拼写为https://delivery-us-central-1.openx.net:443/,它将正常工作。但也许您还想捕获httplib.HTTPException 错误并显示合适的错误消息。
      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 2017-02-06
      • 2012-09-25
      • 1970-01-01
      相关资源
      最近更新 更多