beautifulsoup庫簡單抓取網頁--獲取所有鏈接例子


簡介:

   通過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')


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章