![](/img/trans.png)
[英]Documentation/Examples of using Selenium & Python to navigate a website
[英]Examples given in the Python BeautifulSoup 4 Documentation
我正在學習 BeautifulSoup 4 文檔,並想練習給出的示例。
我正在嘗試這些示例,但它並不成功。 下面舉個例子。
看來我的方式不對,問題出在“網址”上。 放置它們的正確方法是什么?
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.