国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python抓取京東商城手機(jī)列表url實(shí)例代碼

2020-02-23 05:01:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

代碼如下:
#-*- coding: UTF-8 -*-
'''
Created on 2013-12-5

@author: good-temper
'''

import urllib2
import bs4
import time

def getPage(urlStr):
    '''
                獲取頁(yè)面內(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-頁(yè)碼-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'

    #是否有下一頁(yè)
    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');

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高青县| 石家庄市| 陆丰市| 新巴尔虎左旗| 辰溪县| 井陉县| 滦南县| 师宗县| 灌南县| 鄯善县| 五常市| 衡山县| 瑞昌市| 广德县| 东明县| 来宾市| 小金县| 文山县| 桂阳县| 龙里县| 沐川县| 华蓥市| 梅河口市| 宣恩县| 五常市| 沁源县| 简阳市| 石景山区| 金堂县| 丹寨县| 班玛县| 密云县| 克拉玛依市| 宽城| 丽水市| 方山县| 屏山县| 山西省| 石狮市| 雅安市| 墨竹工卡县|