1. 程式人生 > >使用簡單的python語句編寫爬蟲 定時拿取信息並存入txt

使用簡單的python語句編寫爬蟲 定時拿取信息並存入txt

item line 簡單 ror article 5.5 quest win tail

# -*- coding: utf-8 -*-    #解決編碼問題
import urllib
import urllib2
import re
import os
import time

page = 1
url = ‘http://www.qiushibaike.com/text/page/4/?s=4970196‘ #爬取的目標網站
user_agent = ‘Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)‘
headers = { ‘User-Agent‘ : user_agent }
try:
request = urllib2.Request(url,headers = headers)
response = urllib2.urlopen(request)
# print response.read()
content = response.read().decode(‘utf-8‘) #解決編碼問題
pattern = re.compile(r‘<div.*?class="content".*?<span>(.*?)</span>.*?</div>‘,re.S) #第一個參數是匹配要爬取的內容,這裏使用正則去匹配
items = re.findall(pattern,content)
f=open(r‘.\article.txt‘,‘ab‘) #txt文件路徑
nowTimes = time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time())) #獲取當前時間
f.write(‘時間:{}\n\n‘.format(nowTimes),); #txt文件中寫入時間
for i in items:
i.encode(‘utf-8‘)
agent_info = u‘‘.join(i).encode(‘utf-8‘).strip()
f.writelines(‘段子:%s%s\n‘%(str(agent_info),os.linesep)) #分行存入
# f.write(‘%s‘%str(agent_info))
f.close()

# print items

except urllib2.URLError, e:
if hasattr(e,"code"):
print e.code
if hasattr(e,"reason"):
print e.reason


布置定時任務使用crontab。 (具體crontab使用方法可見http://blog.csdn.net/daivon_up/article/details/71266814):

使用簡單的python語句編寫爬蟲 定時拿取信息並存入txt