python提取网页内容_用python提取网页的部分内容

我建议使用BeautifulSoup来解析和搜索html。这将比进行基本的字符串搜索容易得多。在

下面是一个示例,它提取了在包含Legal Authority:标记中找到的所有标记。(请注意,我使用requests库来获取页面内容-这只是一个推荐的、非常容易使用的替代urlopen。)import requests

from BeautifulSoup import BeautifulSoup

# fetch the content of the page with requests library

url = "http://www.reginfo.gov/public/do/eAgendaViewRule?pubId=200210&RIN=1205-AB16"

response = requests.get(url)

# parse the html

html = BeautifulSoup(response.content)

# find all the tags

a_tags = html.findAll('a', attrs={'class': 'pageSubNavTxt'})

def fetch_parent_tag(tags):

# fetch the parent

tag of the first tag

# whose "previous sibling" is the Legal Authority: tag.

for tag in tags:

sibling = tag.findPreviousSibling()

if not sibling:

continue

if sibling.getText() == 'Legal Authority:':

return tag.findParent()

# now, just find all the child

# i.e. finding the parent of one child, find all the children

parent_tag = fetch_parent_tag(a_tags)

tags_you_want = parent_tag.findAll('a')

for tag in tags_you_want:

print 'statute: ' + tag.getText()

如果这不是您需要做的,那么BeautifulSoup仍然是您可能希望用来筛选html的工具。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值