windows定時執行百度新聞爬蟲

來源:互聯網
上載者:User

標籤:oca   news   cut   ring   comm   like   exec   header   find   

  想要做個新聞文本識別分類的項目,就先寫了個爬取百度新聞的爬蟲。

  環境:win7 32 bit python3.4 若干第三方庫

  可以實現的功能:定期按照百度新聞的分類抓取新聞的標題,所屬類別及常值內容,並自動存入資料庫(MySQL),同時發郵件到自己郵箱提醒。

  缺陷:因新聞來源不同,網頁編碼不同,會出現少量的亂碼現象;存入資料庫未添加自動去重功能(自己手動去重其實也並不難,所以沒去研究這個)

  STEP1: creat_dbtable.py連結資料庫建立表(也可直接通過操作MySQL)

# -*- coding: utf-8 -*-"""Created on Sun Nov  6 23:31:33 2016@author: Administrator"""#資料庫建立操作import MySQLdb#開啟資料庫連結db = MySQLdb.Connect(host="localhost",user="root",passwd=‘你的密碼‘,db="test",use_unicode=True, charset="utf8")cursor = db.cursor()#如果資料已經存在,使用excute()方法刪除表cursor.execute("DROP TABLE IF EXISTS news")#建立資料表SQL語句sql = """CREATE TABLE news(class VARCHAR(10) NOT NULL,title VARCHAR(100),text VARCHAR(15000))"""cursor.execute(sql)#關閉資料庫連接db.close()

  在MySQL看到表已經產生:

 

  step2:為了瞭解每次的抓取情況,寫一個send_email.py來實現發送郵件的功能,這個檔案在spider主檔案裡面來調用。

  NOTE:這個往自己的郵箱發送郵件要在相應郵箱開啟服務擷取一個password才可以,這個網上教程也比較多,之後有空會補充。

#coding:utf-8from email.header import Headerfrom email.mime.text import MIMETextfrom email.utils import parseaddr, formataddrimport smtplibdef _format_addr(s):    name, addr = parseaddr(s)    return formataddr((Header(name,‘utf-8‘).encode(), addr))def send_ms(T):    from_addr = "[email protected]"    password = ‘your-password‘    to_addr = ‘[email protected]‘    smtp_server = ‘smtp.qq.com‘    msg = MIMEText(T, ‘plain‘, ‘utf-8‘)    msg[‘From‘] = _format_addr(‘Anyone‘)    msg[‘To‘] = _format_addr(‘Echo‘)    msg[‘Subject‘] = Header(‘The New Report‘, ‘utf-8‘).encode()    server = smtplib.SMTP_SSL(smtp_server, 465, timeout=10)    server.set_debuglevel(0)    server.login(from_addr,password)    server.sendmail(from_addr, [to_addr], msg.as_string())    server.quit()# send_ms(T)

  step3:建立spider.py檔案,實現具體功能。

# -*- coding: utf-8 -*-"""Created on Sun Nov  6 21:24:27 2016@author: Administrator"""import reimport timeimport requestsimport numpy as npimport send_emailfrom bs4 import BeautifulSoupfrom collections import Counterimport MySQLdbstart = time.time()#開啟資料庫連結db = MySQLdb.Connect(host="localhost",user="root",passwd=‘password‘,db="test",use_unicode=True, charset="utf8")cursor = db.cursor()headers = {‘User-Agent‘:"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"}# 擷取首頁資料head_datadef get_head_data():    head_url = ‘http://internet.baidu.com/‘    data = requests.get(head_url,headers=headers)    data.encoding = ‘gbk‘    # print(data.status_code)    head_data = data.text    return head_data# 擷取各新聞分類的title及hrefdef get_class(head_data):    title_href = {}    pa = re.compile(r‘<a href="(http.*?.com/).*?>.*?(\w+)</a></li>‘)    ma = re.findall(pa,head_data)[1:-7]    ma = list(set(ma))[:-1]    # print(len(ma))    for i in range(len(ma)):        key = ma[i][1]        value = ma[i][0]        title_href[key] = value    # print(title_href)    return title_href# 對於每個分類提取標題資訊class_datadef get_class_data(class_url):    class_data = requests.get(class_url, headers=headers)    pa = re.compile(r‘charset=(.*?)">‘)    charset = re.findall(pa,class_data.text)[0]    class_data.encoding  = charset    # class_data.encoding = ‘gbk‘    class_data =class_data.text    soup = BeautifulSoup(class_data, ‘lxml‘)    data = soup.findAll(‘a‘,{‘target‘:‘_blank‘})    class_data = {}    for i in range(len(data)):        title = data[i].get_text()        href = data[i].get(‘href‘)        if len(title) > 10:            if not ‘下載‘ in title:                class_data[title] = href    return class_data# 擷取每條新聞的具體常值內容,粗略抓取def get_news_text(href):    try:        data = requests.get(href,headers=headers)        # data.encoding = ‘gbk‘        pa = re.compile(r‘charset=(.*?)">‘)        charset = re.findall(pa,data.text)[0]        data.encoding  = charset        data = BeautifulSoup(data.text,‘lxml‘).get_text()        text = re.sub("[A-Za-z0-9\[\`\~\!\@\#\$\ \^\"\-\+\_\\&\\n\\t\*\(\)\=\|\{\}\‘\:\;\‘\,\[\]\.\<\>\/\?\~\!\@\#\\\&\*\%]", "", data)    except:        # print(‘get New Text fail...‘)        text = None        pass    return texthead_data = get_head_data()title_href = get_class(head_data)count = 0for class_title,class_href in dict(title_href).items():    print(class_title)    # try:    class_data = get_class_data(class_href)    # except:    #     print(‘get Class data fail...‘)    #     pass    for news_title, news_url in class_data.items():        # print(news_title)        text = get_news_text(news_url)        sql = """INSERT INTO news   SET class=%s, title=%s, text=%s"""        try:            cursor.execute(sql,(class_title,news_title,text))            db.commit()            count += 1        except:            # print(‘Save fail...‘)            passdb.close()end = time.time()total_time = end - startT1 = ‘本次抓取耗時%s‘%str(total_time)T2 = ‘  &   本次共抓取%s條新聞‘%str(count)T = T1+T2# print(t1,t2)send_email.send_ms(T)

  資料庫儲存情況:

  郵件詳情:

 

  REMARK:關於windows定時任務,請參考這篇教程。

  這是我自己計劃任務的設定和運行情況

 

 

windows定時執行百度新聞爬蟲

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.