天天看點

python3 scrapy 爬蟲實戰之爬取站長之家

爬取目标

站長之家:http://top.chinaz.com/all/

爬取工具

win10 python3 scrapy BeautifulSoup

爬取内容

1 網站縮略圖 2 網站名稱 3 網址 4 Alexa排名,5 百度權重 6 網站簡介 7 網站得分
python3 scrapy 爬蟲實戰之爬取站長之家

爬取理由

想着可以通過網站top 來注冊一下 .app的域名,同時這也是一個頂級域名。亦或者進行一下資料分析,看下以後做哪種類型的網站會稍微有前途些(異想天開.gif)

爬取代碼

因為用scrapy 用得熟練,這裡隻貼spider代碼,其他工程代碼,留言索取,即可。
  • spider代碼
# -*- coding: utf-8 -*-
# @Time    : 2018/5/17 19:21
# @Author  : 蛇崽
# @Email   : [email protected]
# @File    : chinaztopSpider.py(網站總排名)
import scrapy
from bs4 import BeautifulSoup


class ChinaztopSpider(scrapy.Spider):
    name = 'chinaztop'

    allowed_domains = ['top.chinaz.com']

    start_urls = ['http://top.chinaz.com/all/']

    count = 
    def parse(self, response):
        soup = BeautifulSoup(response.body,'lxml')
        li_list = soup.find('ul',class_='listCentent').find_all('li',class_='clearfix')
        for li in li_list:
            left_img = li.find('div',class_='leftImg').find('img',class_='')['src']
            detail_site_info = li.find('div',class_='leftImg').find('a',class_='')['href']

            site_com = li.find('h3',class_='rightTxtHead').find('span',class_='col-gray').get_text()
            rtc_data_alexa = li.find('p',class_='RtCData').find('a',class_='').get_text()
            site_info = li.find('p',class_='RtCInfo').get_text()
            print(response.urljoin(left_img))
            print(response.urljoin(detail_site_info))
            print(site_com)
            print(rtc_data_alexa)
            print(site_info)

        # 查找下一頁
        next_links = soup.find('div',class_='ListPageWrap').find_all('a')
        for next_link in next_links:
            text = next_link.get_text()
            print('next-link   =====  ',text)
            if '>' in text:
                nt_link = next_link['href']
                nt_link =response.urljoin(nt_link)
                print('nt_link =====  ',nt_link)
                self.count = self.count +
                print('==============   目前抓取頁數: ',self.count)
                yield scrapy.Request(nt_link,callback=self.parse)
           

這裡的資料暫時隻做列印,日後進行複盤。

爬取部分截圖:

python3 scrapy 爬蟲實戰之爬取站長之家

更多原創文章請通路:https://blog.csdn.net/xudailong_blog

繼續閱讀