python 抓取网页--用BeautifulSoup

</pre><pre code_snippet_id="526653" snippet_file_name="blog_20141120_2_7668334" name="code" class="python"># -*- coding: utf8 -*-
import sys,urllib,re
from HTMLParser import HTMLParser
from bs4 import BeautifulSoup


class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.titleo=''
        self.divFlag=False


    def handle_data(self,data):
        if self.titleo=='':
            self.titleo+=data


url="http://www.putclub.com/html/radio/VOA/presidentspeech/index.html"
wp=urllib.urlopen(url)
content=wp.read()


pattern=re.compile('href="/html([\S\s]*?).html')
hrefs=pattern.findall(content)


for i in range(0,len(hrefs)):
    ##print "http://www.putclub.com/html"+hrefs[i]+".html"
    url="http://www.putclub.com/html"+hrefs[i]+".html"
    wp=urllib.urlopen(url)
    content=wp.read()
    pattern=re.compile('<div class="title">(\S*?)</div>')
    title=pattern.findall(content)


    if len(title)!=0:
        #确定编码
        soup=BeautifulSoup(content,from_encoding='gb2312')
        #查找结点
        body=soup.findAll('div',id='textbody')
        soup=BeautifulSoup(str(body[0]),from_encoding='gb2312')
        paras=soup.findAll('p')
        soupt=BeautifulSoup(str(title[0]),from_encoding='gb2312')
        
        demo=MyHTMLParser()
        demo.feed(title[0])


        #file=open("D:\\Program Files\\presidentspeech\\" + demo.titleo + ".txt",'w')
        file=open("D:\\Program Files\\presidentspeech\\" + soupt.get_text().encode('utf8').decode('utf8') + ".txt",'w')
        #获取标签内容
        file.write(soupt.get_text().encode('utf8')+'\n')
        file.write(url+'\n')
        #file.write(str(paras))
        file.write(soup.get_text().encode('utf8'))
        demo.close()
        file.close()


print "finish"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值