使用簡單的python語句編寫爬蟲 定時拿取資訊並存入txt
阿新 • • 發佈:2019-01-08
echo2.py
# -*- 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
* */1 * * * /usr/bin/python /home/dengwen/desktop/echo2.py