HTTP错误409:冲突urllib2 python

时间:2015-07-29 12:23:39

标签: python api urllib2 http-error

我想用不同的?get变量(x1,....,xn)遍历url api请求列表。

from urllib2 import urlopen, Request
import pandas as pd

var=[x[0],...,x[n]]
url_list=['http://BLAH/get?var[0]&fmt=csv','http://BLAH/get?var[1]&fmt=csv',...      ...,'http://BLAH/get?var[n]&fmt=csv']

j=0
while j < len(url_list):
    req=Request(url_list[j])
    response=urlopen(req)
    df=pd.read_csv(response)
    print df
    j=j+1**

我尝试过del req和response.close()但是我的代码仍然会产生冲突错误。

 File "C:\Users\Anaconda\lib\urllib2.py", line 127, in urlopen
  return _opener.open(url, data, timeout)
 File "C:\Users\Anaconda\lib\urllib2.py", line 410, in open
  response = meth(req, response)
 File "C:\Users\Anaconda\lib\urllib2.py", line 523, in http_response
  'http', request, response, code, msg, hdrs)
 File "C:\Users\nfitzsimons\Anaconda\lib\urllib2.py", line 448, in error
   return self._call_chain(*args)
 File "C:\Users\Anaconda\lib\urllib2.py", line 382, in _call_chain
   result = func(*args)
 File "C:\Users\Anaconda\lib\urllib2.py", line 531, in http_error_default
   raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    rllib2.HTTPError: HTTP Error 409: Conflict

有人有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果您只是尝试迭代网址列表并在成功请求时打印内容,为什么不试试呢?

#!/usr/bin/env python

try:
    import requests
except ImportError as err:
    print("Woops, you're missing " + str(err))


urls = []

req = requests

for url in urls:
   response =  req.get(url)
   if response.status_code == 200: #Successful request
       print(response.content)