2013-08-28 108 views
0

我有這樣的代碼:如何獲取頁面中的所有應用程序鏈接?

from bs4 import BeautifulSoup 
import urllib 

url = 'http://www.brothersoft.com/windows/mp3_audio/midi_tools/' 
html = urllib.urlopen(url) 
soup = BeautifulSoup(html) 

for a in soup.select('div.freeText dl a[href]'): 
    print "http://www.borthersoft.com"+a['href'].encode('utf-8','replace') 

我得到的是:

http://www.borthersoft.com/synthfont-159403.html 
http://www.borthersoft.com/midi-maker-23747.html 
http://www.borthersoft.com/keyboard-music-22890.html 
http://www.borthersoft.com/mp3-editor-for-free-227857.html 
http://www.borthersoft.com/midipiano---midi-file-player-recorder-61384.html 
http://www.borthersoft.com/notation-composer-32499.html 
http://www.borthersoft.com/general-midi-keyboard-165831.html 
http://www.borthersoft.com/digital-music-mentor-31262.html 
http://www.borthersoft.com/unisyn-250033.html 
http://www.borthersoft.com/midi-maestro-13002.html 
http://www.borthersoft.com/music-editor-free-139151.html 
http://www.borthersoft.com/midi-converter-studio-46419.html 
http://www.borthersoft.com/virtual-piano-65133.html 
http://www.borthersoft.com/yamaha-9000-drumkit-282701.html 
http://www.borthersoft.com/virtual-midi-keyboard-260919.html 
http://www.borthersoft.com/anvil-studio-6269.html 
http://www.borthersoft.com/midicutter-258103.html 
http://www.borthersoft.com/softick-audio-gateway-55913.html 
http://www.borthersoft.com/ipmidi-161641.html 
http://www.borthersoft.com/d.accord-keyboard-chord-dictionary-28598.html 

應該是打印出526個應用程序鏈接。 但我只得到二十? 什麼是不足夠的代碼?

回答

1

頁面中只有20個應用程序鏈接。

你必須遍歷所有網頁,以獲取所有鏈接:

from bs4 import BeautifulSoup 
import urllib 

for page in range(1, 27+1): # currently there are 27 pages. 
    url = 'http://www.brothersoft.com/windows/mp3_audio/midi_tools/{}.html'.format(page) 
    html = urllib.urlopen(url) 
    soup = BeautifulSoup(html) 

    for a in soup.select('div.freeText dl a[href]'): 
     print "http://www.borthersoft.com"+a['href'].encode('utf-8','replace') 
+0

哦man..how可我想不出that..thanks人.. –

+0

那麼如果自動申請頁面增加? –

+0

@wanmohdpayed,我不明白你的評論問題。你可能不想要硬編碼頁數('27')?然後,自己解析頁碼。如果有更多的頁面繼續到下一頁,或者中斷。 – falsetru

相關問題