簡介:
通過BeautifulSoup 的 find_all方法,找出所有a標籤中的href屬性中包含http的內容,這就是我們要找的網頁的一級鏈接( 這裏不做深度遍歷鏈接)
並返回符合上述條件的a標籤的href屬性的內容,這就是我們要找的某個網頁的所帶有的一級鏈接
#!/opt/yrd_soft/bin/python import re import urllib2 import requests import lxml from bs4 import BeautifulSoup url = 'http://www.baidu.com' #page=urllib2.urlopen(url) page=requests.get(url).text pagesoup=BeautifulSoup(page,'lxml') for link in pagesoup.find_all(name='a',attrs={"href":re.compile(r'^http:')}): #print type(link) print link.get('href')