AttributeError:“ dict”对象在检查图片网址时没有属性“ decode”

问题描述

我正在尝试检查360,000个图像URL的列表并计算有效图像。如果范围是1000个项目,则下面的迭代效果很好,但是如果将范围提高到5000或更多,它将显示错误“ AttributeError:'dict'对象没有属性'decode'”。

谁能帮助我修复错误和/或修改代码以一次运行遍历所有360,000个URL?非常感谢!


# Here's my codes

cnt_1 = 0

for post in list_post[0:5000]:
    try:
        url = post['photo_url']
        parse_object = urlparse(url)
        conn = http.client.HTTPConnection(parse_object.netloc)
        conn.request('HEAD',parse_object.path)
        response = conn.getresponse()
        conn.close()
        if (response.status == 200):
            cnt_1 += 1
    except AttributeError:
        continue

# The below is the error I received
---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
<ipython-input-670-065df3328ecc> in <module>
      6         parse_object = urlparse(url)
      7         conn = http.client.HTTPConnection(parse_object.netloc)
----> 8         conn.request('HEAD',parse_object.path)
      9         response = conn.getresponse()
     10         conn.close()

/opt/anaconda3/lib/python3.7/http/client.py in request(self,method,url,body,headers,encode_chunked)
   1250                 encode_chunked=False):
   1251         """Send a complete request to the server."""
-> 1252         self._send_request(method,encode_chunked)
   1253 
   1254     def _send_request(self,encode_chunked):

/opt/anaconda3/lib/python3.7/http/client.py in _send_request(self,encode_chunked)
   1296             # default charset of iso-8859-1.
   1297             body = _encode(body,'body')
-> 1298         self.endheaders(body,encode_chunked=encode_chunked)
   1299 
   1300     def getresponse(self):

/opt/anaconda3/lib/python3.7/http/client.py in endheaders(self,message_body,encode_chunked)
   1245         else:
   1246             raise CannotSendHeader()
-> 1247         self._send_output(message_body,encode_chunked=encode_chunked)
   1248 
   1249     def request(self,body=None,headers={},*,/opt/anaconda3/lib/python3.7/http/client.py in _send_output(self,encode_chunked)
   1024         msg = b"\r\n".join(self._buffer)
   1025         del self._buffer[:]
-> 1026         self.send(msg)
   1027 
   1028         if message_body is not None:

/opt/anaconda3/lib/python3.7/http/client.py in send(self,data)
    964         if self.sock is None:
    965             if self.auto_open:
--> 966                 self.connect()
    967             else:
    968                 raise NotConnected()

/opt/anaconda3/lib/python3.7/http/client.py in connect(self)
    936         """Connect to the host and port specified in __init__."""
    937         self.sock = self._create_connection(
--> 938             (self.host,self.port),self.timeout,self.source_address)
    939         self.sock.setsockopt(socket.IPPROTO_TCP,socket.TCP_NODELAY,1)
    940 

/opt/anaconda3/lib/python3.7/socket.py in create_connection(address,timeout,source_address)
    705     host,port = address
    706     err = None
--> 707     for res in getaddrinfo(host,port,SOCK_STREAM):
    708         af,socktype,proto,canonname,sa = res
    709         sock = None

/opt/anaconda3/lib/python3.7/socket.py in getaddrinfo(host,family,type,flags)
    750     # and socket type values to enum constants.
    751     addrlist = []
--> 752     for res in _socket.getaddrinfo(host,flags):
    753         af,sa = res
    754         addrlist.append((_intenum_converter(af,AddressFamily),

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...