2017-04-26 47 views
0

首先讓我解釋來源: 我正在寫一個簡單的python腳本,在網站的所有頁面中搜索並收集帶有文本的特殊html標籤。 我的代碼:停止python腳本的過程?

lineline = urllib.request.urlopen("http://www.test-site.com") 
lineliner = lineline.read() 
allsoupurl = beautifulsoup(lineliner, "html.parser") 
allhtmllisturl = allsoupurl.find_all("h1", class_= "title") 

print (allhtmllisturl) 

OK,這個代碼,工作非常好,顯示與級冠軍所有可用的H1標籤。結果是:

[<h1 class="title>title-1</h1>"] 
[<h1 class="title>title-2</h1>"] 
[<h1 class="title>title-3</h1>"] 
[<h1 class="title>title-4</h1>"] 

,但是當我改變這樣的代碼:

lineline = urllib.request.urlopen("http://www.test-site.com") 
lineliner = lineline.read() 
allsoupurl = beautifulsoup(lineliner, "html.parser") 
allhtmllisturl = allsoupurl.find_all("h1", class_= "title") 

for h1 in allhtmllisturl: 
    print (h1.get_text()) 

腳本的結果只顯示第一個可用的(H1)標籤,然後腳本結束,不顯示所有可用的標籤。 而且結果是:

title-1 

是什麼問題?

感謝

+0

是什麼類型(allhtmllisturl)和allhtmllisturl.shape? –

+0

'

title-1

」''爲什麼你的課程名稱沒有用'''括起來,而是在'「之後關閉,這個必須像'

title-1

' –

+0

我無法用我的本地設置(pyhon3,beautifulsoup4.5.3)重現問題。你能否提供你使用的python和beautifoulsoup版本? – Catalin

回答

0

內部find_all()元件具有一些的ID必須是內部ATTRS = {}(屬性)

lineline = urllib.request.urlopen("http://www.test-site.com") 
lineliner = lineline.read() 
allsoupurl = beautifulsoup(lineliner, "html.parser") 
allhtmllisturl = allsoupurl.find_all("h1", attrs={'class'= "title"}) 

for h1 in allhtmllisturl: 
    print (h1.get_text()) 
+0

如果它解決了您的問題,然後接受答案 –

+0

tnx。但沒有解決 - @ nishant-kumar –