欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python抓取京東商城手機列表url實例代碼

 更新時間:2013年12月18日 15:25:21   作者:  
python抓取京東商城手機列表url實例分享,大家參考使用吧

復(fù)制代碼 代碼如下:

#-*- coding: UTF-8 -*-
'''
Created on 2013-12-5

@author: good-temper
'''

import urllib2
import bs4
import time

def getPage(urlStr):
    '''
                獲取頁面內(nèi)容
    '''
    content = urllib2.urlopen(urlStr).read()
    return content

def getNextPageUrl(currPageNum):
    #http://list.jd.com/9987-653-655-0-0-0-0-0-0-0-1-1-頁碼-1-1-72-4137-33.html
    url =  u'http://list.jd.com/9987-653-655-0-0-0-0-0-0-0-1-1-'+str(currPageNum+1)+'-1-1-72-4137-33.html'

    #是否有下一頁
    content = getPage(url);
    soup = bs4.BeautifulSoup(content)
    list = soup.findAll('span',{'class':'next-disabled'});
    if(len(list) == 0):
        return url
    return ''

def analyzeList():
    pageNum = 0
    list = []
    url = getNextPageUrl(pageNum)
    while url !='':
        soup = bs4.BeautifulSoup(getPage(url))
        pagelist = soup.findAll('div',{'class':'p-name'})
        for elem in pagelist:
            soup1 =  bs4.BeautifulSoup(str(elem))
            list.append(soup1.find('a')['href'])

        pageNum = pageNum+1
        print pageNum
        url = getNextPageUrl(pageNum)
    return list

def analyzeContent(url):

    return ''

def writeToFile(list, path):
    f = open(path, 'a')
    for elem in list:
        f.write(elem+'\n')
    f.close()

if __name__ == '__main__':
    list = analyzeList()
    print '共抓取'+str(len(list))+'條\n'

    writeToFile(list, u'E:\\jd_phone_list.dat');

相關(guān)文章

  • 在python image 中安裝中文字體的實現(xiàn)方法

    在python image 中安裝中文字體的實現(xiàn)方法

    今天小編大家分享一篇在python image 中安裝中文字體的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Django實現(xiàn)翻頁的示例代碼

    Django實現(xiàn)翻頁的示例代碼

    翻頁是經(jīng)常使用的功能,Django提供了翻頁器。用Django的Paginator類實現(xiàn),有需要了解Paginator類用法的朋友可參考。希望此文章對各位有所幫助
    2021-05-05
  • Python用棧實現(xiàn)隊列的基本操作

    Python用棧實現(xiàn)隊列的基本操作

    隊列(Queue)和棧(Stack)是常見的數(shù)據(jù)結(jié)構(gòu),它們在計算機科學中有著廣泛的應(yīng)用,在Python中,可以使用列表(List)來實現(xiàn)棧,但要用棧來實現(xiàn)隊列需要一些巧妙的操作,本文就給大家詳細介紹一下Python中如何用棧實現(xiàn)隊列,需要的朋友可以參考下
    2023-11-11
  • Python寫的貪吃蛇游戲例子

    Python寫的貪吃蛇游戲例子

    這篇文章主要介紹了Python寫的貪吃蛇游戲例子,練手作品,又好玩又可以學到東西,需要的朋友可以參考下
    2014-06-06
  • 最新評論