【发布时间】:2013-06-04 10:47:09
【问题描述】:
我正在抓取我的 Android 应用所需的 webpage。我想做的是从href 属性中提取国家。这与one 相同。
这是我的代码:
from bs4 import BeautifulSoup
import urllib2
import re
html_page = urllib2.urlopen("http://www.howtocallabroad.com/a.html")
soup = BeautifulSoup(html_page)
li = soup.select("ul > li > a")
for link in li:
print link.get('href')
我遇到的问题是结果返回所有a 标记,包括来自其他divs 的标记
afghanistan/
albania/
algeria/
american-samoa/
andorra/
angola/
anguilla/
antigua/
argentina/
armenia/
aruba/
ascension/
australia/
austria/
azerbaijan/
codes.html # not needed
nanp.html # not needed
qa/ # not needed
forums/ # not needed
我想知道完成这项工作需要什么功能。我只想在<div id="content"> 中过滤hrefs。 docs 没有太多信息。
对不起,这是我第一次写python。
【问题讨论】:
标签: python html beautifulsoup