欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 九一免费在线观看 | 日本高清免费zzzzzzzz | www伊人 | 在线一级片 | 精品国产午夜福利在线观看 | 久久久久久中文字幕 | 精品一区二区国语对白 | 久久经典视频 | 国产一区二区欧美 | 国产日韩欧美一区 | 成人免费播放视频777777 | 精品伊人久久久 | 一级一级 a爱片免费视频 | 免费看一级欧美毛片视频 | 两性视频网 | 亚洲国产成人在线视频 | 大片毛片 | 中文字幕在线一区二区三区 | 欧美性受 | 亚洲午夜久久久精品影院 | 色婷婷99综合久久久精品 | 毛片激情永久免费 | 国产成人免费永久播放视频平台 | 久久精品国产亚洲 | 欧美成人一区二区三区在线视频 | 精品亚洲国产成av人片传媒 | 国产精品久久久久久免费软件 | 91在线亚洲精品专区 | 五月天色婷婷在线 | 一级做性色a爰片久久毛片 亚洲午夜精品久久久久久app | 欧亚乱熟女一区二区在线 | 中文字幕一区二区视频 | 欧美大片在线免费观看 | 亚洲视频在线看 | 欧美日韩视频在线第一区 | 免费男女视频 | 精品无人区乱码一区二区三区手机 | 青娱乐手机在线 | 免费a级在线观看播放 | 中文字幕一区二区在线观看 | 三级毛片在线看 |