您的位置:首页 > 编程语言 > Python开发

Python 爬虫入门

2016-12-18 09:39 162 查看
获取网页

#-*- coding: UTF-8 -*-
__author__ = 'yzzls'
import urllib
import urllib2

http_url = "http://www.henshiyong.com"  #需要登录的网址
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'  #有些服务器或 Proxy 会通过该值来判断是否是浏览器发出的请求
#反盗链,服务器会识别headers中的referer是不是它自己,如果不是,有的服务器不会响应,在headers中加入referer即可解决
#http_headers = { 'User-Agent': user_agent, 'Referer':http_url }
http_headers = {'User-Agent': user_agent}  #设置headers内的属性
http_value = {"username": "XXXm", "password": "XXX"}
http_data = urllib.urlencode(http_value)  #利用urllib的urlencode方法将字典编码
http_request = urllib2.Request(http_url, http_data, http_headers)  #构建一个请求
#捕获异常例如404异常等
try:
http_response = urllib2.urlopen(http_request)  #响应请求
except urllib2.URLError, e:
if hasattr(e, "code"):
print e.code
if hasattr(e, "reason"):
print e.reason
else:
print "OK"
print http_response.read()#通过read()方法返回网页内容


cookie

#-*- coding: UTF-8 -*-
__author__ = 'yzzls'
import urllib
import urllib2
import cookielib

# 设置保存cookie的文件,同级目录下的cookie.txt
filename = 'cookie.txt'
# 声明一个MozillaCookieJar对象实例来保存cookie,之后写入文件
http_cookie = cookielib.MozillaCookieJar(filename)

# 声明一个CookieJar对象实例来保存cookie,直接将获得的cookie保存到系统内存中
# http_cookie = cookielib.CookieJar()

# 利用urllib2库的HTTPCookieProcessor对象来创建cookie处理器
http_handler=urllib2.HTTPCookieProcessor(http_cookie)
# 通过http_handler来构建opener
http_opener = urllib2.build_opener(http_handler)
# 此处的open方法同urllib2的urlopen方法,也可以传入request
http_response = http_opener.open('http://www.baidu.com')
# 保存cookie到文件
# ignore_discard的意思是即使cookies将被丢弃也将它保存下来
# ignore_expires的意思是如果在该文件中cookies已经存在,则覆盖原文件写入
http_cookie.save(ignore_discard=True, ignore_expires=True)

# 读取保存在txt文件中的cookie信息

# 先创建MozillaCookieJar实例对象,用于保存在内存中
read_cookie = cookielib.MozillaCookieJar()
# 从文件中读取cookie内容到变量
read_cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)
# 创建一个新的request请求
new_request = urllib2.Request("http://www.baidu.com")
# 利用urllib2的build_opener方法创建一个opener
new_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(read_cookie))
new_response = new_opener.open(new_request)
# print new_response.read()

# 如果我们的 cookie.txt 文件中保存的是某个人登录百度的cookie,那么我们提取出这个cookie文件内容,就可以用以上方法模拟这个人的账号登录百度。

# 实例应用,用cookie模拟网站登录,创建一个带有cookie的opener,
# 在访问登录的URL时,将登录后的cookie保存下来,然后利用这个cookie来访问其他网址。
filename2 = 'cookie2.txt'
# 声明一个MozillaCookieJar对象实例来保存cookie,之后写入文件
cookie = cookielib.MozillaCookieJar(filename2)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
post_data = urllib.urlencode( {'stuid': '201200131012', 'pwd': '23342321'} )
# 登录教务系统的URL
loginUrl = 'http://jwxt.sdu.edu.cn:7890/pls/wwwbks/bks_login2.login'
# 模拟登录,并把cookie保存到变量
result = opener.open(loginUrl, post_data)
# 保存cookie到cookie.txt中
cookie.save(ignore_discard=True, ignore_expires=True)
# 利用cookie请求访问另一个网址,此网址是成绩查询网址
gradeUrl = 'http://jwxt.sdu.edu.cn:7890/pls/wwwbks/bkscjcx.curscopre'
# 请求访问成绩查询网址
result = opener.open(gradeUrl)
print result.read()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息