环境:python3.7.2
尝试用Socket爬虫网站:http://desk.zol.com.cn/
附代码:
#https://desk-fd.zol-img.com.cn/t_s960x600c5/g5/M00/00/07/ChMkJl3qNKaIDNA2AARqqK0FxbEAAvnJAJbLQMABGrA592.jpg
import socket
b=socket.socket()
b.connect(('desk-fd.zol-img.com.cn',80))
req_line='GET /t_s960x600c5/g5/M00/00/07/ChMkJl3qNKaIDNA2AARqqK0FxbEAAvnJAJbLQMABGrA592.jpg HTTP/1.1\r\n'
req_header='host:desk-fd.zol-img.com.cn\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36\r\nReferer: http://desk.zol.com.cn/bizhi/8376_103845_2.html\r\n'
empty_line='\r\n'
req_content=''
b.send((req_line+req_header+empty_line+req_content).encode())
complete_content=b''
res_content=b.recv(4096)
while res_content:
complete_content+=res_content
res_content=b.recv(4096)
print(len(res_content))
with open('d:/temp2.jpg','wb')as f:
f.write(complete_content)
执行结果:正常
问题:图片和网上下载的大小一样,打开提示不支持此格式
用网页下载的图一正常打开