Python BeautifulSoup 4文档中给出的示例

问题描述 投票:2回答:1

我正在学习BeautifulSoup 4文档,并希望执行给出的示例。

我正在尝试示例,但没有成功。下面的例子。

似乎我的输入方式不正确,问题出在'url'。放置它们的正确方法是什么?

from bs4 import BeautifulSoup
import re
import urllib2


url = '<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>'

page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

Learning = soup.find_all("a", class_="sister")

print Learning
python beautifulsoup
1个回答
2
投票

'<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>'不是网址。

代码包含html;您不需要使用urllib2.urlopen

from bs4 import BeautifulSoup

page = '<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>'
soup = BeautifulSoup(page)
Learning = soup.find_all("a", class_="sister")
print Learning
© www.soinside.com 2019 - 2024. All rights reserved.