Python爬虫基础

1.爬虫入门程序

一、什么是爬虫?

        一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息。

二、Python爬虫架构

Python 爬虫架构主要由五个部分组成,分别是调度器、URL管理器、网页下载器、网页解析器、应用程序(爬取的有价值数据)。

扒取网页和基本代码:

import urllib2

response = urllib2.urlopen("http://www.baidu.com")
print response.read()

2.爬虫程序添加data、header,然后post请求

一、添加data,header代码如下

import urllib  
import urllib2  

url = 'http://www.server.com/login'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'  
values = {'username' : 'cqc',  'password' : 'XXXX' }  
headers = { 'User-Agent' : user_agent }  
data = urllib.urlencode(values)  
request = urllib2.Request(url, data, headers)  
response = urllib2.urlopen(request)  
page = response.read()

二、POST请求

代码如下:<

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