鱼C论坛

 找回密码
 立即注册
查看: 751|回复: 8

为什么requests的代理不能用,如果用urllib.request就行

[复制链接]
发表于 2018-7-10 07:47:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. import urllib.request
  3. import random
  4. import re
  5. import os

  6. def install():
  7.     users = [
  8.     "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0",
  9.     "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; Maxthon/3.0)",
  10.     "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ;  QIHU 360EE)"
  11.     ]
  12.     ips = [
  13.     "http://61.191.41.130",
  14.     "http://122.238.12.191",
  15.     "http://222.161.56.166"
  16.     ]
  17.     ip = random.choice(ips)
  18.     user = random.choice(users)
  19.     return ip,user
  20.    
  21. def get_html(url):
  22.     num = install()
  23.     rep = requests.get(url,data={'User-Agent':num[1]},proxies={'https':num[0]},stream=True)
  24.     return rep

  25. name = input('请输入要查找的商品:')
  26. num = int(input('请输入页数:'))

  27. key = urllib.request.quote(name)

  28. os.mkdir(name)
  29. os.chdir(name)
  30.    
  31. def main():
  32.     for j in range(num):
  33.         url = 'https://s.taobao.com/search?q=' + key + '&s='+ str(j*44)
  34.         html = get_html(url).text
  35.         s = re.findall(r'"pic_url":"([^"]+?)".+?"view_price":"([^"]+?)".+?"view_sales":"([^"]+?)"',html)
  36.         
  37.         
  38.         os.mkdir('%s%d'%(name,j+1))
  39.         os.chdir('%s%d'%(name,j+1))
  40.    
  41.         for i in s:
  42.             img = get_html('http:' + str(i[0]))#在这获取html不能使用代理
  43.             print(img)
  44.             
  45.             with open('%s元_%s.jpg'%(i[1],i[2]),'wb') as f:
  46.                 f.write(img.content)
  47.    
  48.         os.chdir(os.pardir)
  49.         
  50. if __name__ == '__main__':
  51.     main()


  52.   

复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-10 09:31:38 | 显示全部楼层
iwanna 发表于 2018-7-10 09:29
可能是你ip是http,proxies里面写的是https
另外,user-angent是放在data里吗,我记得应该放在headers里{: ...

是headers,我弄错了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-10 09:41:50 | 显示全部楼层
iwanna 发表于 2018-7-10 09:29
可能是你ip是http,proxies里面写的是https
另外,user-angent是放在data里吗,我记得应该放在headers里{: ...

爬一张图还行,多张图就不行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-10 10:02:06 | 显示全部楼层
iwanna 发表于 2018-7-10 09:58
不行是什么不行,报的什么错

raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPConnectionPool(host='222.161.56.166', port=80): Max retries exceeded with url: http://g-search1.alicdn.com/img/ ... !0-saturn_solar.jpg (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000021D734E6B38>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-10 10:46:35 | 显示全部楼层
我没办法,只能这样了

  1. import requests
  2. import urllib.request as u
  3. import random
  4. import re
  5. import os

  6. def get_html(url):
  7.     user = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
  8.     ips = [
  9.     '222.161.56.166',
  10.     '122.238.12.191',
  11.     '61.191.41.130'
  12.         ]
  13.     ip = random.choice(ips)
  14.     p = u.ProxyHandler({'http':ip})
  15.     opener = u.build_opener(p)
  16.     u.install_opener(opener)
  17.    
  18.     rep = requests.get(url,headers={'User-Agent':user})
  19.     return rep

  20. name = input('请输入要查找的商品:')
  21. num = int(input('请输入页数:'))

  22. key = u.quote(name)

  23. os.mkdir(name)
  24. os.chdir(name)
  25.    
  26. def main():
  27.     for j in range(num):
  28.         url = 'https://s.taobao.com/search?q=' + key + '&s='+ str(j*44)
  29.         html = get_html(url).text
  30.         s = re.findall(r'"pic_url":"([^"]+?)".+?"view_price":"([^"]+?)".+?"view_sales":"([^"]+?)"',html)
  31.         
  32.         os.mkdir('%s%d'%(name,j+1))
  33.         os.chdir('%s%d'%(name,j+1))
  34.         
  35.         for i in s:
  36.             img = get_html(url=('http:' + i[0]))
  37.             
  38.             with open('%s元_%s.jpg'%(i[1],i[2]),'wb') as f:
  39.                 f.write(img.content)
  40.    
  41.         os.chdir(os.pardir)
  42.         
  43. if __name__ == '__main__':
  44.     main()


  45.   

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-3 15:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表