鱼C论坛

 找回密码
 立即注册
查看: 1387|回复: 4

[已解决]爬某网站妹子图老是报错

[复制链接]
发表于 2018-2-19 21:42:37 | 显示全部楼层 |阅读模式
10鱼币
  1. import urllib.request as q
  2. import os,re
  3. import urllib.error

  4. def readurl(url):
  5.     head={}

  6.     head['User-Agent']='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)'
  7.     req=q.Request(url,headers=head)
  8.     try:
  9.         res=q.urlopen(url)
  10.     except urllib.error.HTTPError as cwu:
  11.         return cwu.code
  12.     except urllib.error.URLError as ucwu:
  13.         return ucwu.code

  14.     html=res.read()
  15.     return html

  16. def xiazai():
  17.     for j in range(1,445):
  18.         if j<10:
  19.             motenum='00'+str(j)
  20.         elif 10<=j<100:
  21.             motenum='0'+str(j)
  22.         else:
  23.             motenum=str(j)
  24.         
  25.         url=r'http://www.ugirls.com/Content/List/Magazine-'+ motenum+'.html'#模特总共从001到444
  26.         
  27.         html=readurl(url)
  28.         if type(html)==type(int()):
  29.             
  30.             if 400<=html<=599:
  31.                 print('网页错误无法下载%d'%html)
  32.                 continue
  33.         else:
  34.             html=html.decode('utf-8')
  35.             #获取模特名字
  36.             uname=re.findall(r'<meta name="keywords" content="[\u4e00-\u9fa5]{2,5}、ugirls ([\u4e00-\u9fa5]{2,3})" />',html)
  37.             if len(uname)==0:
  38.                 uname='大集合'
  39.             else:
  40.                 uname=str(uname[0])
  41.             print('第%d模特是%s'%(j,uname))
  42.             
  43.             dqian=os.getcwd()
  44.             try:
  45.                 os.mkdir('ugirl')
  46.             except OSError:
  47.                
  48.                 os.chdir('ugirl')
  49.             else:
  50.                 os.chdir('ugirl')

  51.             try:
  52.                 os.mkdir(uname)
  53.             except OSError:
  54.                 os.chdir(uname)
  55.             else:
  56.                 os.chdir(uname)

  57.             str1=len('05c9b11a01aebf2a2af18a030ffea553')
  58.             str2=len('_magazine_web_m.jpg')
  59.             imglist=re.findall(r'http://img.ugirls.tv/uploads/magazine/content/[^"]+_magazine_web_m\.jpg',html)
  60.             for each in imglist:
  61.                 with open(each.split('/')[-1][:str1]+'.jpg','wb') as f:
  62.                     f.write(readurl(each[:-str2]+'.jpg'))
  63.                 print(each[:-str2]+'.jpg',end='下载完成\n')
  64.             print('******************************************************************')
  65.             os.chdir(dqian)

  66. xiazai()
复制代码

寂寞的兄弟有福利了
最佳答案
2018-2-19 21:42:38
  1. res=q.urlopen(url)
复制代码

这一行应该是
  1. res=q.urlopen(req)
复制代码
好烦啊.png

最佳答案

查看完整内容

这一行应该是
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-19 21:42:38 | 显示全部楼层    本楼为最佳答案   
  1. res=q.urlopen(url)
复制代码

这一行应该是
  1. res=q.urlopen(req)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-19 22:28:13 From FishC Mobile | 显示全部楼层
°蓝鲤歌蓝 发表于 2018-2-19 22:15
这一行应该是

可以运行,就是隔一段时间报错一次,明天照你说的地方改改再,现在睡觉了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-20 00:03:37 | 显示全部楼层
楼上说的req的确是一个问题,如果直接用不用req伪装user-agent,他会直接拒绝你的请求的,但是单单加上一个请求头,过一段时间他一样会根据你的IP地址拒绝你的访问,所以你需要使用代理iP,具体的用法小甲鱼有教程的,免费的IP可以去哪些代理网站上爬取
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-20 08:33:22 | 显示全部楼层
我也经常遇到这种问题,要不就加代理,要不就用最简单的办法
把请求的这段换成这样
  1. def readurl(url):
  2.     head={}

  3.     head['User-Agent']='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)'
  4.     req=q.Request(url,headers=head)
  5.     try:
  6.         res=q.urlopen(url)
  7.     except Exception:
  8.         readurl(url)
  9.     html=res.read()
  10.     return html
复制代码


遇到错误就重新请求一次
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-17 05:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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