码迷,mamicode.com
首页 > Web开发 > 详细

第一个网络爬虫——简单的抓取网页

时间:2015-06-13 01:06:05      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

早上还有实验验收,先上代码,早上再写。

 

import urllib2
import re
from bs4 import BeautifulSoup

content = urllib2.urlopen("http://www.cnblogs.com/ly941122/").read();
soup=BeautifulSoup(content)
siteUrls = soup.findAll(‘div‘,{‘class‘:‘postTitle‘})
tag=re.compile(‘</?\w+[^>]*>‘)
print siteUrls 
file=open(‘res.txt‘,‘wb‘)
try:
    for i in siteUrls:
        str1=re.sub(tag,‘ ‘,str(i))
        str1=str1[1:]
        # print str1
        str2=str(i).split(‘ ‘)
        html = str2[2][6:-1]
        file.write(str1+html+‘\n‘)
except Exception,e:
    print e
file.close()
raw_input()

 

第一个网络爬虫——简单的抓取网页

标签:

原文地址:http://www.cnblogs.com/ly941122/p/4572861.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!