【问题标题】:How to get the name of the url with BeautifulSoup如何使用 BeautifulSoup 获取 url 的名称
【发布时间】:2014-02-11 15:52:28
【问题描述】:

如何使用 BeautifulSoup 获取 url 的名称。 我有这个代码:

from BeautifulSoup import BeautifulSoup
import urllib2
import re

html_page = urllib2.urlopen("http://www.youtube.com")
soup = BeautifulSoup(html_page)
list =  soup.findAll('div', attrs={'class':'profileBox'})

for div in list:
    print div.a['href']

---------------------------------
<a href="/sam">sam utx</a>
-------------------------------------

这打印 href("/sam") 但我需要的是 url 的名称 (sam utx)。 我怎样才能做到这一点?

【问题讨论】:

    标签: django web-scraping beautifulsoup urllib2


    【解决方案1】:

    您可以在div 中选择text 本身:

    div.a.string
    

    您可以阅读有关此here 的更多信息。

    【讨论】:

      猜你喜欢
      • 2011-01-30
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      相关资源
      最近更新 更多