2

「次へ」ボタンのURLを取得し、ページがなくなるまでループし続けるときに、最初にページからすべてのリンクを取得しようとしています。それを達成するためにネストされたループを取得しようとしましたが、何らかの理由で BeautifulSoup は 2 番目のページを解析せず、最初のページのみを解析してから停止します。

説明するのは難しいですが、これは私が説明しようとしていることを理解しやすいコードです:)

#this site holds the first page that it should start looping on.. from this page i want to reach page 2, 3, etc.
   webpage = urlopen('www.first-page-with-urls-and-next-button.com').read()

soup = BeautifulSoup(webpage)

for tag in soup.findAll('a', { "class" : "next" }):

    print tag['href']
    print "\n--------------------\n"


#next button is relative url so append it to main-url.com
    soup = BeautifulSoup('http://www.main-url.com/'+ re.sub(r'\s', '', tag['href']))

#for some reason this variable only holds the tag['href']
    print soup

    for taggen in soup.findAll('a', { "class" : "homepage target-blank" }):
        print tag['href']

        # Read page found
        sidan = urlopen(taggen['href']).read()

# get title
        Titeln = re.findall(patFinderTitle, sidan)

        print Titeln

何か案は?下手な英語で申し訳ありませんが、私が叩かれないことを願っています:)私がそれを下手に説明したかどうか尋ねてください。もう少し説明するために最善を尽くします。ああ、私はPythonが初めてです-今日の時点で(あなたが考えたかもしれません:)

4

2 に答える 2

2

新しいURLを呼び出しurlopenて、結果のファイルオブジェクトをBeatifulSoupに渡すと、すべて設定されていると思います。あれは:

wepage = urlopen(http://www.main-url.com/'+ re.sub(r'\s', '', tag['href']))
soup = BeautifulSoup(webpage)
于 2012-04-26T20:05:32.730 に答える
0

ラインの場合:

soup = BeautifulSoup('http://www.main-url.com/'+ re.sub(r'\s', '', tag['href']))

試す:

webpage = urlopen('http://www.main-url.com/'+re.sub(r'\s','',tag['href'])).read()

soup = BeautifulSoup(webpage)

于 2012-04-26T20:05:42.793 に答える