简体   繁体   English

Python BeautifulSoup 4 文档中给出的示例

[英]Examples given in the Python BeautifulSoup 4 Documentation

I am learning the BeautifulSoup 4 Documentation, and want to exercise the examples given.我正在学习 BeautifulSoup 4 文档,并想练习给出的示例。

I am trying the examples however it's not successful.我正在尝试这些示例,但它并不成功。 An example below.下面举个例子。

It seems I am not putting it in the right way, and problem lies in the 'url'.看来我的方式不对,问题出在“网址”上。 What is the right way to put them?放置它们的正确方法是什么?

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>' is not an url. '<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>'不是网址。

The code contains html;代码包含html; You don't need to use urllib2.urlopen .您不需要使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM