Python 爬蟲執行個體(7)—— 爬取 新浪軍事新聞

來源:互聯網
上載者:User

標籤:技術   port   lte   set   hand   turn   使用   com   host   

我們開啟新浪新聞,看到頁面如下,首先去爬取一級 url,圖片中藍色圓圈部分

                         

第二zh張圖片,顯示需要分頁,

           

 

 

原始碼:

# coding:utf-8import jsonimport redisimport timeimport requestssession = requests.session()import logging.handlersimport pickleimport sysimport reimport datetimefrom bs4 import BeautifulSoupimport sysreload(sys)sys.setdefaultencoding(‘utf8‘)import datetime# 產生一年的日期def dateRange(start, end, step=1, format="%Y-%m-%d"):    strptime, strftime = datetime.datetime.strptime, datetime.datetime.strftime    days = (strptime(end, format) - strptime(start, format)).days    return [strftime(strptime(start, format) + datetime.timedelta(i), format) for i in xrange(0, days, step)]def spider():    date_list = dateRange("2017-01-01", "2018-01-06")[::-1]    print date_list    for date in date_list:        for page in range(1,5):            #組合url            url = "http://roll.mil.news.sina.com.cn/col/zgjq/" + str(date)+"_"+ str(page) +".shtml"            # 偽裝要求標頭            headers = {                "Host":"roll.mil.news.sina.com.cn",                              "Cache-Control":"max-age=0",                "Upgrade-Insecure-Requests":"1",                "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36",                "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",                              "Accept-Encoding":"gzip, deflate",                "Accept-Language":"zh-CN,zh;q=0.8",                "If-Modified-Since":"Sat, 06 Jan 2018 09:57:24 GMT",            }            result = session.get(url=url,headers=headers).content            #編碼格式是 gb2312,使用BeautifulSoup解決編碼格式            soup = BeautifulSoup(result,‘html.parser‘)            #找到新聞列表            result_div = soup.find_all(‘div‘,attrs={"class":"fixList"})[0]            #去下換行            result_replace = str(result_div).replace(‘\n‘,‘‘).replace(‘\r‘,‘‘).replace(‘\t‘,‘‘)            #正則匹配資訊            result_list = re.findall(‘<li>(.*?)</li>‘,result_replace)            for i in result_list:                #匹配出來新聞 url, name,time                news_url = re.findall(‘<a href="(.*?)" target=‘,i)[0]                news_name = re.findall(‘target="_blank">(.*?)</a>‘,i)[0]                news_time = re.findall(‘<span class="time">\((.*?)\)</span>‘,i)[0]                print news_url                print news_name                print news_timespider()

 

Python 爬蟲執行個體(7)—— 爬取 新浪軍事新聞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.