鱼C论坛

 找回密码
 立即注册
查看: 2310|回复: 3

以下代码怎么改,才能实现用自动收集的IP来访问网页吖?

[复制链接]
发表于 2015-12-31 14:11:02 | 显示全部楼层 |阅读模式
100鱼币
本帖最后由 欣欣celin 于 2016-1-3 13:18 编辑
  1. import urllib.request as r
  2. from urllib.error import URLError
  3. import random
  4. import re


  5. iplist = {} #存放IP和端口

  6. def open_url(url='http://www.xicidaili.com/wn/'):
  7.     req = r.Request(url)
  8.     req.add_header('User-Agent','Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36')
  9.     try:
  10.         page = r.urlopen(req)
  11.     except URLError as e:
  12.         if hasattr(e, 'reason'):
  13.             print('we failed to reach a server,')
  14.             print('reson: ', e.reason)
  15.         elif hasattr(e, 'code'):
  16.             print('the server could not fulfill the request,')
  17.             print('error code:', e.code)
  18.     else:
  19.         html = page.read().decode('utf-8')
  20.         return html


  21. def get_ip(html):
  22.     p = r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])'
  23. #'?:'表示:非bu获组
  24.     #要修正:iplist.append(re.findall(p, html))
  25.     return iplist

  26. def url_opener(url):   
  27.     req = r.Request(url)
  28.     req.add_header('User-Agent','Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36')

  29.     proxies = get_ip(open_url(url))
  30.     proxy = random.choice(proxies)
  31.     proxy_support = r.ProxyHandler({'http':proxy})

  32.     opener = r.build_opener(proxy_support)
  33.     r.install_opener(opener)
  34.    
  35.     try:
  36.         response = r.urlopen(url)
  37.     except URLError as e:
  38.         if hasattr(e, 'reason'):
  39.             print('we failed to reach a server,')
  40.             print('reson: ', e.reason)
  41.         elif hasattr(e, 'code'):
  42.             print('the server could not fulfill the request,')
  43.             print('error code:', e.code)        
  44.     else:
  45.         html2 = response.read()

  46.         return html2

  47. if __name__=='__main__':
  48.     url = input('请输入要打开的网页:')
  49.     url_opener(url)
  50.     print(html2)
  51.    
复制代码

最佳答案

查看完整内容

致命错误: 1. 27行预编译正则表达式语法错误,需要使用re.compile()来预编译. 2. 36行open_url()函数不应该传入参数,而是应该使用该函数的参数的默认值 3. 60行print一个函数内的变量,严重错误!应该print(url_opener(url)) 语法规范: 1. PEP8的规范我就不谈了,基础的基础. 2. 第9行定义函数使用的形参和外部定义的变量重名,易引起帮你维护代码的人(我)的疑问. 3. 17和48行的reason不知道是故意还是为什么拼写成了r ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-12-31 14:11:03 | 显示全部楼层
致命错误:
1. 27行预编译正则表达式语法错误,需要使用re.compile()来预编译.
2. 36行open_url()函数不应该传入参数,而是应该使用该函数的参数的默认值
3. 60行print一个函数内的变量,严重错误!应该print(url_opener(url))
语法规范:
1. PEP8的规范我就不谈了,基础的基础.
2. 第9行定义函数使用的形参和外部定义的变量重名,易引起帮你维护代码的人(我)的疑问.
3. 17和48行的reason不知道是故意还是为什么拼写成了reson
个人建议:
只是学习的话可以练练手写匹配ip地址的正则表达式,我观察了你给的获取代理的网页,代理分http和https两种,你可以重新写一个正则表达式来分开匹配这两种协议的代理ip,至于使用就不要太勉强,毕竟是免费的,不出错就很给面子了.
我的代码:
  1. import urllib.request as r
  2. from urllib.error import URLError
  3. import random
  4. import re

  5. RE_IP = re.compile(r'(?:(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[0,1]?\d?\d|2[0-4]\d|25[0-5])')


  6. def open_url(url='http://www.xicidaili.com/wn/'):
  7.     req = r.Request(url)
  8.     req.add_header('User-Agent',
  9.                    'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36')
  10.     try:
  11.         page = r.urlopen(req)
  12.     except URLError as e:
  13.         if hasattr(e, 'reason'):
  14.             print('we failed to reach a server,')
  15.             print('reson: ', e.reason)
  16.         elif hasattr(e, 'code'):
  17.             print('the server could not fulfill the request,')
  18.             print('error code:', e.code)
  19.     else:
  20.         html = page.read().decode('utf-8')
  21.         return html


  22. def get_ip(html):
  23.     ip_list = RE_IP.findall(html)
  24.     return ip_list


  25. def url_opener(url):
  26.     req = r.Request(url)
  27.     req.add_header('User-Agent',
  28.                    'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36')

  29.     proxies = get_ip(open_url())
  30.     proxy = random.choice(proxies)
  31.     proxy_support = r.ProxyHandler({'http': proxy})

  32.     opener = r.build_opener(proxy_support)
  33.     r.install_opener(opener)

  34.     try:
  35.         response = r.urlopen(url)
  36.     except URLError as e:
  37.         if hasattr(e, 'reason'):
  38.             print('we failed to reach a server,')
  39.             print('reson: ', e.reason)
  40.         elif hasattr(e, 'code'):
  41.             print('the server could not fulfill the request,')
  42.             print('error code:', e.code)
  43.     else:
  44.         html2 = response.read()

  45.         return html2


  46. if __name__ == '__main__':
  47.     link = input('请输入要打开的网页:')
  48.     print(url_opener(link))
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +5 收起 理由
冬雪雪冬 + 5 + 5 + 5 感谢楼主无私奉献!

查看全部评分

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

使用道具 举报

发表于 2016-1-2 13:21:50 | 显示全部楼层
。,。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-1-3 13:18:57 | 显示全部楼层
hldh214 发表于 2015-12-31 14:11
致命错误:
1. 27行预编译正则表达式语法错误,需要使用re.compile()来预编译.
2. 36行open_url()函数不应 ...

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-21 02:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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