BeautifulSoup demo

# coding:utf8
from bs4 import BeautifulSoup
import urllib2

url = "http://www.baidu.com"
response = urllib2.urlopen(url)
str = response.read()

soup = BeautifulSoup(str,
                     'html.parser',
                     from_encoding='utf-8')
print '获取所有链接'
links = soup.find_all('a')
for link in links:
    print link.name, link["href"], link.get_text()

你可能感兴趣的:(python)