ホームページ  >  記事  >  バックエンド開発  >  Python は京東モールの携帯電話リストの URL サンプル コードをキャプチャします

Python は京東モールの携帯電話リストの URL サンプル コードをキャプチャします

WBOY
WBOYオリジナル
2016-06-16 08:45:501334ブラウズ

コードをコピー コードは次のとおりです:

#-*- コーディング: UTF-8 -*-
''
2013-12-5 作成

@作者: 良い気性
'''

urllib2 をインポート
bs4 をインポート
時間のインポート

def getPage(urlStr):
'''
ページ コンテンツを取得します
'''
content = urllib2.urlopen(urlStr).read()
コンテンツを返します

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);
スープ = bs4.BeautifulSoup(content)
リスト =Soup.findAll('span',{'class':'next-disabled'});
if(len(list) == 0):
return url
return ''

defanalystList():
pageNum = 0
list = []
url = getNextPageUrl(pageNum)
while url != '' :
スープ = bs4.BeautifulSoup(getPage(url))
ページリスト =Soup.findAll('div',{'class':'p-name'})
ページリストの要素:
Soup1 = BS4.Beautifulsoup (Str (ELEM))
List.append (soup1.find ('a') ['href'])

Pagenum = Pagenum+1
PRINT PAGENUM
url = getNextPageUrl(pageNum)
リストを返します

def applyContent(url):

return ''

def writeToFile(list, path):
f = open(path, 'a')
リスト内の要素:
f.write(elem+'n')
f.close ()

if __name__ == '__main__':
list =analyzeList()
print 'クロールされた合計'+str(len(list))+'n'

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

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。