python - Python BeautifulSoup 4 文档中给出的示例

标签 python beautifulsoup

我正在学习 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

最佳答案

'<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

关于python - Python BeautifulSoup 4 文档中给出的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225640/

相关文章:

python - 在 .so 文件中使用 C 模块时出现段错误

python -\r(回车)在 Python 中如何工作

python - 两个文本文件之间的百分比差异

python - BeautifulSoup 。元素索引错误

python - 使用 BS4、Selenium 在 Python 中抓取动态数据并避免重复

python - 如何用beautifulsoup提取h1标签文本

python - 解析 Scrapy 中的相邻项

python - 您可以将 `appsettings.json` 部署到 python(v2 编程模型)中的 azure 函数吗?

python - WTForms - DateTimeLocalField 数据在提交后为 None

Python Beautifulsoup 表 td 元素