您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容主要讲解“一个很水的Python代码分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“一个很水的Python代码分析”吧!
# _ * _编码:UTF-8 _ * _ “””xianhu的python_spider.py“”” 导入请求import urllib.errorimport urllib.parseimport urllib.request导入 http.cookiejar
第一段
#首先定义下边可能需要的变量
url = “ https://www.baidu.com ”headers = { “ User-Agent ”:“ Mozilla / 4.0(兼容; MSIE 5.5; Windows NT)” }
#最简单的网页抓取方式
response = urllib.request.urlopen(url,timeout = 10)
html = response.read()。decode(“ utf-8 ”)
#使用请求实例代替网址
request = urllib.request.Request(url,data = None,headers = {})
response = urllib.request.urlopen(request,timeout = 10)第二段
#发送数据,即在请求()中添加数据参数
data = urllib.parse.urlencode({ “ act ”:“ login ”,“ email ”:“ xianhu@qq.com ”,“ password ”:“ 123456 ” })
request1 = urllib.request.Request(URL,数据=数据) # POST方法
请求2 = urllib.request.Request(URL + “?%S ” %数据) # GET方法
response = urllib.request.urlopen(request,timeout = 10)
#发送报头,即在请求()中添加报头参数
request = urllib.request.Request(url,data = data,headers = headers) #参数中添加header参数
request.add_header(“ Referer ”,“ http://www.baidu.com ”) #另一种添加header的方式,添加Referer是为了应对“反盗链”response = urllib.request.urlopen(request,timeout = 10)第三段
#网页抓取引发异常:urllib.error.HTTPError,urllib.error.URLError,两者存在继承关系
尝试:
urllib.request.urlopen(request,timeout = 10)
除了 urllib.error.HTTPError 为 e:
打印(e.code,e.reason)
除了 urllib.error.URLError 为 e:
打印(e.errno,e.reason)
#使用代理,以防止IP被封或IP次数受限:
proxy_handler = urllib.request.ProxyHandler(proxies = { “ http ”:“ 111.123.76.12:8080 ” })
opener = urllib.request.build_opener(proxy_handler) #利用代理创建opener实例
响应= opener.open(URL) #直接利用开启器实例打开URL
urllib.request.install_opener(opener) #安装全局opener,然后利用urlopen打开网址
response = urllib.request.urlopen(url)第四段
#使用饼干和cookiejar,应对服务器检查 cookie_jar = http.cookiejar.CookieJar() cookie_jar_handler = urllib.request.HTTPCookieProcessor(cookiejar = cookie_jar) opener = urllib.request.build_opener(cookie_jar_handler) response = opener.open(url) #发送在浏览器中获取的饼干,两种方式: #(1)直接放到headers里 headers = { “ User-Agent ”: “ Mozilla / 4.0(兼容; MSIE 5.5; Windows NT)”, “ Cookie ”: “ PHPSESSID = btqkg9amjrtoeev8coq0m78396; USERINFO = n6nxTHTY%2BJA39z6CpNB4eKN8f0KsYLjAQTwPe%2BhLHLruEbjaeh5ulhWAS5RysUM%2B; ” } request = urllib.request.Request(url,headers = headers) #(2)构建cookie,添加到cookiejar中 cookie = http.cookiejar.Cookie(name = “ xx ”,value = “ xx ”,domain = “ xx ”,...) cookie_jar.set_cookie(饼干) response = opener.open(url)
#同时使用代理和cookiejar opener = urllib.request.build_opener(cookie_jar_handler) opener.add_handler(proxy_handler) response = opener.open(“ https://www.baidu.com/ ”)#抓取网页中的图片:同样适用于抓取网络上的文件右击鼠标,找到图片属性中的地址,然后进行保存。 response = urllib.request.urlopen(“ http://ww3.sinaimg.cn/large/7d742c99tw1ee7dac2766j204q04qmxq.jpg ”,超时= 120)使用 open(“ test.jpg ”,“ wb ”)作为 file_img: file_img.write(response.read()) # HTTP认证:即HTTP身份验证 password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() #创建一个PasswordMgr password_mgr.add_password(realm = None,uri = url,user = ' username ',passwd = ' password ') #添加用户名和密码 handler = urllib.request.HTTPBasicAuthHandler(password_mgr) #创建HTTPBasicAuthHandler opener = urllib.request.build_opener(handler) #创建opner response = opener.open(url,timeout = 10) #获取数据 #使用套接字代理 进口袜子 导入套接字 socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5,“ 127.0.0.1 ”,1080) socket.socket = socks.socksocket requests.get(“ http://www.baidu.com/s?ie=utf-8&wd=ip ”)
到此,相信大家对“一个很水的Python代码分析”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。