nameerror:name 'html_doc' is not defined
nameerror:name 'html_doc' is not defined是怎么回事啊?
nameerror:name 'html_doc' is not defined是怎么回事啊?
 
                            2016-02-29
需要先定义
#encoding:utf-8
from bs4 import BeautifulSoup
import re
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(
        html_doc,
        'html.parser',
        from_encoding = 'utf-8')
links = soup.find_all('a')
# 获取所有超链接的元素
for link in links:
    print link.name,link['href'],link.get_text()
# 获取某个超链接的元素
link_node = soup.find('a', href = 'http://example.com/tillie')
print '某个元素:',link.name,link['href'],link.get_text()
# 正则表达式模糊匹配获取元素
link_node = soup.find('a', re.compile(r'sie'))
print '正则匹配:',link.name,link['href'],link.get_text()
# 获取指定的属性的节点
link_node = soup.find('p', class_ = 'title')
print '属性匹配:',link.name,link.get_text()举报