日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python爬蟲獲取網站數據

系統 1770 0

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

            
               	pip install requests
    pip install beautifulsoup4
    pip install xlwt

            
          

具體的實現類GetInfo.py

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

'''
實際下載方法
'''
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);
       
    '''
        獲取商品具體的內容
    '''
    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=''
        
            #保存爬取的數據
            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 = ['產品類目', '系列', '產品名稱', '型號', '產品描述', '廠家','產品成交價']  # 表頭
        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 = []
    #保存所有要爬的網頁
    urlList = []

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

	# 網址去重,防止數據重復
    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個網址' % i)
        dl.init('https://www....', item)
        dl.get_page_urls()
        dl.get_prodect_urls();
        dl.get_contents(productList)

    dl.writer(productList)

            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 正安县| 库尔勒市| 泽库县| 浏阳市| 伊宁县| 绥棱县| 青浦区| 平顺县| 泽州县| 清徐县| 柳江县| 南召县| 尤溪县| 江阴市| 墨竹工卡县| 南安市| 青河县| 饶阳县| 郑州市| 泸定县| 车险| 嘉鱼县| 迭部县| 榆树市| 西平县| 城口县| 富川| 黄骅市| 神池县| 太康县| 天水市| 张家口市| 大新县| 东源县| 白水县| 卓尼县| 凤庆县| 奇台县| 西藏| 渝中区| 克山县|