python爬取模块_python爬虫----(1. 基本模块)

python爬虫,web spider。爬取网站获取网页数据,并进行分析提取。

基本模块使用的是 urllib,urllib2,re,等模块

(一)基本用法,例子

(1)进行基本GET请求,获取网页html

#!coding=utf-8

import urllib

import urllib2

url = ‘http://www.baidu.com/‘

# 获取请求

request = urllib2.Request(url)

try:

# 根据request,得到返回response

response = urllib2.urlopen(request)

except urllib2.HTTPError, e:

if hasattr(e, ‘reason‘):

print e.reason

# 读取response的body

html = response.read()

# 读取response的headers

headers = response.info()

(2)表单提交

#!coding=utf-8

import urllib2

import urllib

post_url = ‘‘

post_data = urllib.urlencode({

‘username‘: ‘username‘,

‘password‘: ‘password‘,

})

post_headers = {

‘User-Agent‘: ‘Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:31.0) Gecko/20100101 Firefox/31.0‘,

}

request = urllib2.Request(

url=post_url,

data=post_data,

headers=post_headers,

)

response = urllib2.urlopen(request)

html = response.read()

(3)

#!coding=utf-8

import urllib2

import re

page_num = 1

url = ‘http://tieba.baidu.com/p/3238280985?see_lz=1&pn=‘+str(page_num)

myPage = urllib2.urlopen(url).read().decode(‘gbk‘)

myRe = re.compile(r‘class="d_post_content j_d_post_content ">(.*?)

‘, re.DOTALL)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值