您的位置:首页 > 编程语言 > Python开发

python python 入门学习之网页数据爬虫cnbeta文章保存

2015-01-25 22:19 429 查看
需求驱动学习的动力。

因为我们单位上不了外网所以读新闻是那么的痛苦,试着自己抓取网页保存下来,然后离线阅读。今天抓取的是cnbeta科技新闻,抓取地址是http://m.cnbeta.com/wap/index.htm?page=1,咱们需要抓取的是前5页就行了。代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2,re,time,json
import sys
from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf-8')
n=0
f = open('cnbeta.txt','a')
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
mainurl="http://m.cnbeta.com/wap"

for i in range(1,5):
add='http://m.cnbeta.com/wap/index.htm?page='+str(i)

req = urllib2.Request(add, headers=headers)
wb=urllib2.urlopen(req).read()
soup=BeautifulSoup(wb)

file=open(str(i)+'cnbetamain.html','a')
file.write(wb)
elv1ment=soup.find_all('div',{'class':'list'})
for elv in elv1ment:
n=n+1
url=elv.find('a',href=True).get('href')
name=elv.find('a',href=True).get_text()
print name + ','+'http://m.cnbeta.com'+url
f.write(str(n)+','+name + ','+'http://m.cnbeta.com'+url+'\n')
try:
html =urllib2.urlopen(urllib2.Request('http://m.cnbeta.com'+url, headers=headers)).read()
filename=name+'.html'
file=open(filename,'a')
file.write(html)
except:
print 'NOT FOUND'
#print filename

time.sleep(1)
f.close()
file.close()
print 'OVER'


首先需要抓取页面,循环地址,这个地方需要注意的是因为很多网站禁止机器访问所以需要headers,万能的

headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}

拿到主页数据后需要找到此主页包含的文章及文章地址,用beautifulsoup访问处理html,beautifulsoup需要对网页进行分级处理,有head body div title href几种模式,这里需要用的是 div class="list" 。找到文章地址后打开url并保存到当前文件夹下面,名字用文章名命名。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: