2014-11-05 48 views
-2
from bs4 import BeautifulSoup 
import requests 


r = requests.get("xxx") 
soup = BeautifulSoup(r.content) 
for link in soup.find_all('html'): 
    print link 

這不適合我有人可以幫忙嗎?如何提取所有具有.html末尾的網址?

+0

你的屬性進行搜索,因爲大多數鏈接在HREF或src屬性 – Hackaholic 2014-11-05 21:32:35

回答

-1

您可能想要使用正則表達式並搜索「href」屬性。像這樣的東西可以幫助你開始。假設您正在搜索所有href屬性

import re 
from urllib2 import urlopen 
from bs4 import BeautifulSoup 
tags = soup.find_all(href = re.compile(r"\.html$")) 

標記變量將是所有html標記的列表,其href屬性以.html結尾。現在,你可以遍歷標籤和提取HREF

0
for link in soup.find_all('a'): 
    if '.html' in link['href']: 
     print link 
+0

將只需要鏈接從,重量約IMG,IFRAME等等??? – Hackaholic 2014-11-05 21:40:45

+0

實際上,我想檢索以.html結尾的所有網址,並且當我打印鏈接時顯示爲空 – Bozo 2014-11-06 05:51:41

相關問題