亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

python爬蟲獲取網站數據

系統 1520 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條評論
主站蜘蛛池模板: 中文字幕1区 | 久久久精品波多野结衣 | 一级毛片一级片 | 老子理论不卡影院6080 | 国产成人精品日本亚洲直接 | 99精品国产福利在线观看 | 日本精品a在线 | 99视频精品全国免费 | 国产区在线观看视频 | 婷婷伊人五月 | 亚洲欧洲一二三区机械有限公司 | 一区二区三区在线 | 亚洲精品高清国产麻豆专区 | 欧美日韩成人午夜免费 | 国产在线一区在线视频 | 一级成人毛片免费观看 | 日韩毛片免费线上观看 | 亚洲 欧美 中文字幕 | 久久综合97色综合网 | 亚洲va中文字幕欧美不卡 | 理论片在线观看视频 | 最新国产中文字幕 | 亚洲永久精品ww47 | 免费看操片 | julia紧身裙中文字幕在线看 | 中文字幕网在线 | 四虎影视在线 | 伊人成人在线观看 | 亚洲精品国产一区二区图片欧美 | 99国产精品免费视频 | 日韩一区精品视频在线看 | 91亚洲国产成人精品性色 | 亚洲精品久久久久综合网 | 天天操天天干天天爽 | 99视频精品 | 夜夜女人国产香蕉久久精品 | 玖玖免费 | 欧美日韩成人在线视频 | 国产中日韩一区二区三区 | 骚婷婷| 91久娇草|