欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python爬蟲獲取網(wǎng)站數(shù)據(jù)

系統(tǒng) 1613 0

要爬取的網(wǎng)站不需要登陸,沒有反爬機制,操作很簡單
首先安裝需要的程序包

            
               	pip install requests
    pip install beautifulsoup4
    pip install xlwt

            
          

具體的實現(xiàn)類GetInfo.py

            
              #信息實體類
class product_info(object):
        serios = ''  # 存放商品系列
        productActualPrice = ''  # 存放商品成交價
        productOldPrice = ''  # 存放商品面價
        detailString = ''  # 存放商品詳情
        productCategory = ''  # 產(chǎn)品類目
        productName = ''  # 產(chǎn)品名稱
        productTypeNum = ''  # 產(chǎn)品型號
        productFactory = ''  # 產(chǎn)品廠家

'''
實際下載方法
'''
class downloader(object):

    def __init__(self):
        self.server = ''
        self.target = ''
        self.pageUrls = []  # 存放各個頁面鏈接
        self.productUrls = []  # 存放各個商品鏈接
        self.productInfo = []  # 商品信息列表,用以保存至Excel

    '''
    初始化serverUrl及targetUrl
    '''
    def init(self,serverUrl,targetUrl):
        self.server = serverUrl
        self.target = targetUrl
    '''
    獲取全部的分頁頁面
    '''
    def get_page_urls(self):
        req = requests.get(url=self.target)
        self.pageUrls.append(self.target)
        html = req.text
        div_bf = BeautifulSoup(html, 'html.parser')
        a = div_bf.find_all('div', class_='m-pagination')[0].find_all('a')
        for each in a:
            if each.text!='下一頁' and each.text!='末頁' and each.text!='上一頁' and each.text!='首頁':
                self.pageUrls.append(self.server+each.get('href'))

    '''
       獲取全部商品的url
    '''
    def get_prodect_urls(self):
        for item in self.pageUrls:
            req = requests.get(url=item)
            html = req.text
            bf = BeautifulSoup(html, 'html.parser')
            imageDivs = bf.find('div',id="goodsList",class_="。。。").find_all(class_="。。。")
            for item in imageDivs:
                temp = item.find_all('a')[0].get('href')
                self.productUrls.append(self.server + temp);
       
    '''
        獲取商品具體的內(nèi)容
    '''
    def get_contents(self,productList):
        print('productList長度%d'%len(productList));
        i=0;
        for targetUrl in self.productUrls:
            i += 1
            if i%5==0:
                press = i/len(self.productUrls)*100
                print('爬取進度%f' % press)
            req = requests.get(url=targetUrl)
            html = req.text
            # 整個頁面的soup對象#
            soup = BeautifulSoup(html, 'html.parser')
            # 獲取頁面頭部信息#
            headInfo = soup.find('div', class_='。。。')
            productName = headInfo.find_all('h1', class_="。。。")[0]
            if productName != None:
                productName = headInfo.find_all('h1', class_="。。。")[0].text.strip().replace('\n', '');
            
            productActualPrice = headInfo.find('tr', class_="。。。").find(
                class_="。。。").find('span')
            if productActualPrice != None:
                productActualPrice = headInfo.find('tr', class_="。。。").find(
                    class_="。。。").find('span').text.strip().replace('\n', '');
            
            productTypeNum = headInfo.find('table', class_="。。。").find_all('tr')[1].find('td')
            if productTypeNum != None:
                productTypeNum = headInfo.find('table', class_="。。。").find_all('tr')[1].find(
                    'td').text.strip().replace('\n', '');
            #productWeight = headInfo.find('table', class_="。。。").find_all('tr')[2].find('td').text.strip().replace('\n', '');
            #print(productTypeNum)
            #print(productWeight)
            detailTable=[]
            serios=''
        
            #保存爬取的數(shù)據(jù)
            good = product_info()
            good.serios = serios
            good.productActualPrice = productActualPrice
            good.detailString = detailString
            good.productCategory = '電氣'
            good.productName = productName
            good.productTypeNum = productTypeNum
            good.productFactory = productFactory

            if good not in productList:
                productList.append(good);

    '''
       保存到Excel中
    '''
    def writer(self,productList):
        print('開始寫入excel成功')
        workbook = xlwt.Workbook(encoding='utf-8')
        sheet = workbook.add_sheet('ProdectInfo')
        head = ['產(chǎn)品類目', '系列', '產(chǎn)品名稱', '型號', '產(chǎn)品描述', '廠家','產(chǎn)品成交價']  # 表頭
        for h in range(len(head)):
            sheet.write(0, h, head[h])
        i = 1
        for product in productList:
            sheet.write(i, 0, product.productCategory)
            sheet.write(i, 3, product.serios)
            sheet.write(i, 4, product.productName)
            sheet.write(i, 5, product.productTypeNum)
            sheet.write(i, 6, product.detailString)
            sheet.write(i, 7, product.productFactory)
            sheet.write(i, 9, product.productActualPrice)
            i += 1

        workbook.save('C:/Users/Desktop/.....xls')
        print('寫入excel成功')


if __name__ == "__main__":
    #保存所有爬到的商品信息
    productList = []
    #保存所有要爬的網(wǎng)頁
    urlList = []

	urlList.append('https://www....')
    urlList.append('https://www.....')

	# 網(wǎng)址去重,防止數(shù)據(jù)重復(fù)
    news_ids = []
    for item in urlList:
        if item not in news_ids:
            news_ids.append(item)

    i = 0;
    for item in news_ids:
        dl = downloader()
        i += 1
        print('開始爬取第%d個網(wǎng)址' % i)
        dl.init('https://www....', item)
        dl.get_page_urls()
        dl.get_prodect_urls();
        dl.get_contents(productList)

    dl.writer(productList)

            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 一级大片久久 | 国产精品第1页 | 天天插天天狠天天透 | 天天夜天干天天爽 | 欧美一区不卡 | 99爱在线视频这里只有精品 | 国产精品高清视亚洲乱码 | 热99这里只有精品 | 亚洲高清久久 | 欧美阿v天堂视频在99线 | 在线观看中文字幕 | 久久中文字幕网站篠田优 | 国产视频h| 婷婷激情久久 | 亚洲午夜精品一区二区三区 | 波多野吉衣在线观看 | 亚洲精品欧美一区二区三区 | 97久久精品一区二区三区观看 | 99欧美精品 | 狠狠综合久久av一区二区小说 | 日本一视频一区视频二区 | 99亚洲视频| 精品视频久久 | 日韩亚洲欧美一区 | 福利免费在线 | 偷拍亚洲制服另类无码专区 | 漂流教室免费观看韩国电影 | 大喷水| 毛片基地免费视频a | 亚洲一区色 | 奇米第四色在线观看 | 天天舔天天舔 | 国产精品亚洲一区二区三区在线 | 欧洲精品一区 | 亚洲欧美日韩精品中文乱码 | 日韩有码一区二区三区 | 毛片性生活 | 国产午夜精品久久久久久久蜜臀 | 欧美视频在线观看 | 午夜视频在线免费观看 | 久久国产亚洲欧美日韩精品 |