python爬虫基础

下面是爬取网站源代码的代码,用的我们学校的教务处网站。。

#!/usr/bin/env python

import urllib
import urllib2

url = 'http://etc.sdut.edu.cn/eol/main.jsp'
user_agent='Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0'
values = {}
values['username']='******'
values['password']='******'
headers = {'User-Agent':user_agent}
data = urllib.urlencode(values)
request = urllib2.Request(url)
try:
    response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
    print e.code
except urllib2.URLError, e:
    print e.reason
else:
    print response.read()

你可能感兴趣的:(爬虫,python)