0

我有这个链接:

http://www.brothersoft.com/windows/categories.html

我正在尝试获取 div 中项目的链接。例子:

http://www.brothersoft.com/windows/mp3_audio/midi_tools/

我试过这段代码:

import urllib
from bs4 import BeautifulSoup

url = 'http://www.brothersoft.com/windows/categories.html'

pageHtml = urllib.urlopen(url).read()

soup = BeautifulSoup(pageHtml)

sAll = [div.find('a') for div in soup.findAll('div', attrs={'class':'brLeft'})]

for i in sAll:
    print "http://www.brothersoft.com"+i['href']

但我只得到输出:

http://www.brothersoft.com/windows/mp3_audio/

我怎样才能得到我需要的输出?

4

1 回答 1

2

Urlhttp://www.brothersoft.com/windows/mp3_audio/midi_tools/不在 tag 中<div class='brLeft'>,所以如果 output 是http://www.brothersoft.com/windows/mp3_audio/,那是正确的。

如果你想得到你想要的网址,改变

sAll = [div.find('a') for div in soup.findAll('div', attrs={'class':'brLeft'})]

sAll = [div.find('a') for div in soup.findAll('div', attrs={'class':'brRight'})]

更新:

在“midi_tools”中获取信息的示例

import urllib 
from bs4 import BeautifulSoup

url = 'http://www.brothersoft.com/windows/categories.html'
pageHtml = urllib.urlopen(url).read()
soup = BeautifulSoup(pageHtml)
sAll = [div.find('a') for div in soup.findAll('div', attrs={'class':'brRight'})]
for i in sAll:
    suburl = "http://www.brothersoft.com"+i['href']    #which is a url like 'midi_tools'

    content = urllib.urlopen(suburl).read()
    anosoup = BeautifulSoup(content)
    ablock = anosoup.find('table',{'id':'courseTab'})
    for atr in ablock.findAll('tr',{'class':'border_bot '}):
        print atr.find('dt').a.string      #name
        print "http://www.brothersoft.com" + atr.find('a',{'class':'tabDownload'})['href']   #link
于 2013-08-22T10:42:16.667 回答