python爬虫小实例

使用Python爬取网页数据
本文介绍了一种利用Python中的urllib2库和BeautifulSoup进行网页数据抓取的方法。通过构造请求头和设置代理服务器来获取指定链接的内容,并尝试解析网页以下载图片等资源。
from bs4 import BeautifulSoup
import urllib2
import urllib, os, re, time, sys
#import socket


def build_request(link):
#             user_agent = 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'


                values = {'name' : 'Michael Foord',


                                'location' : 'Northampton',


                                'language' : 'Python' }


                headers = { 'User-Agent' : 'Custom User-Agent' }


                data=urllib.urlencode(values)   


                req = urllib2.Request(link, data, headers)


                req.add_unredirected_header('User-Agent', 'Custom User-Agent')


                return req          


def build_urllib2(link):  


                print time.strftime('%Y-%m-%d_%H:%M:%S',time.localtime(time.time()))              


                #set timeout


                urllib2.socket.setdefaulttimeout(60)               


                #set proxy


                null_proxy_handler = urllib2.ProxyHandler({"http" : 'http://10.159.32.155:8080'})
 #               null_proxy_handler = urllib2.ProxyHandler({"http" : 'http://10.144.1.10:8080'})
 
                null_proxy_handler = urllib2.ProxyHandler({})               


                opener = urllib2.build_opener(null_proxy_handler)               


                urllib2.install_opener(opener)  


                print 'after install opener'


                print time.strftime('%Y-%m-%d_%H:%M:%S',time.localtime(time.time()))     




def get_img(link, path):


                print 'start to download '+ link


                content=[]


                try:


                                print link
                                req= build_request(link)
                                
                                print 'start to build_request'
                                
                                content = urllib2.urlopen(req)


#                             content = opener.open(req)     


                                print 'start to create soup about '
                                print content




                except urllib2.HTTPError, e:
                                
                                print 'Error happened'
                                
                                print e.code


                                print e.msg


                                print e.headers
                                
                                print e.url


                                print e.fp.read()


#weblink = "http://www.163.com"


weblink = "http://qt.gtimg.cn/q=s_sh601899"


#weblink = "http://blog.chinaunix.net/uid-7448695-id-2626493.html"


mypath = "D:\\python\\gif\\test\\img"




build_urllib2(weblink)


get_img(weblink, mypath)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值