鱼C论坛

 找回密码
 立即注册
查看: 1266|回复: 0

[技术交流] 第 54 讲 论一只爬虫的自我修养 2

[复制链接]
发表于 2018-5-3 22:10:55 | 显示全部楼层 |阅读模式

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

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

x
  1. 测试题:
  2. 0.
  3. 为HTTP,HTTPS,FTP类链接请求规定相应时间

  4. 连接超时时间,单位是秒.

  5. 1.
  6. 使用getcode()方法

  7. 2.
  8. POST和GET

  9. 3.
  10. 互相响应

  11. 发出请求的永远是客户端,响应的永远是服务器.

  12. 4.
  13. 请求者的标识符,服务器用来识别访问者是普通浏览器还是代码.

  14. 普通浏览器用这玩意向网站汇报我的浏览器类型,操作系统,浏览器内核等.

  15. 5.
  16. 在urlopen()里面,加入data参数.

  17. 6.
  18. 首先要知道字符串的编码,再用string.decode(编码)转为unicode

  19. 7.
  20. 结构化的字符串形式.

  21. 用字符串把python的数据结构封装起来,方便存储使用

  22. 动动手:
  23. 0.
  24. import urllib.request as r

  25. ##width = 400
  26. ##highth = 600

  27. width = input('请输入宽度:')
  28. highth = input('请输入高度:')

  29. ##file_path = input('请输入保存位置:')

  30. file_path = '/Users/yufan/Documents/python编程/54/cat_%s_%s.png' % (width,highth)

  31. response = r.urlopen('http://placekitten.com/%s/%s' % (width,highth))

  32. ##response = r.urlopen('http://www.placekitten.com/200/300')

  33. html = response.read()

  34. with open(file_path,'wb') as f:
  35.     f.write(html)


  36. 1.
  37. # -- coding:gbk --
  38. import re
  39. import urllib
  40. import http

  41. loginurl = 'https://www.douban.com/accounts/login'
  42. cookie = http.cookiejar()
  43. opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookie))

  44. params = {
  45. "form_email":"your email",
  46. "form_password":"your password",
  47. "source":"index_nav" #没有的话登录不成功
  48. }

  49. #从首页提交登录
  50. response=urllib.request.OpenerDirector.open(loginurl, urllib.urlencode(params))

  51. #验证成功跳转至登录页
  52. if response.geturl() == "https://www.douban.com/accounts/login":
  53.     html=response.read()

  54.     #验证码图片地址
  55.     imgurl=re.search('<img id="captcha_image" src="(.+?)" alt="captcha" class="captcha_image"/>', html)
  56.     if imgurl:
  57.         url=imgurl.group(1)
  58.         #将图片保存至同目录下
  59.         res=urllib.urlretrieve(url, 'v.jpg')
  60.         #获取captcha-id参数
  61.         captcha=re.search('<input type="hidden" name="captcha-id" value="(.+?)"/>' ,html)
  62.         if captcha:
  63.             vcode=raw_input('请输入图片上的验证码:')
  64.             params["captcha-solution"] = vcode
  65.             params["captcha-id"] = captcha.group(1)
  66.             params["user_login"] = "登录"
  67.             #提交验证码验证
  68.             response=urllib.request.OpenerDirector.open(loginurl, urllib.urlencode(params))
  69.             ''' 登录成功跳转至首页 '''
  70.             if response.geturl() == "http://www.douban.com/":
  71.                 print ('login success ! ')
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 00:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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