Python漂亮的汤参数 [英] Python beautiful soup arguments

查看:37
本文介绍了Python漂亮的汤参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以使用BeautifulSoup从页面中获取一些文本

I have this code that fetches some text from a page using BeautifulSoup

soup= BeautifulSoup(html)
body = soup.find('div' , {'id':'body'})
print body

我想使它成为可重用的函数,它需要一些htmltext和与之匹配的标签,如下所示

I would like to make this as a reusable function that takes in some htmltext and the tags to match it like the following

def parse(html, atrs):
 soup= BeautifulSoup(html)
 body = soup.find(atrs)
 return body

但是如果我打这样的电话

But if i make a call like this

    parse(htmlpage, ('div' , {'id':'body'}"))  or like

parse(htmlpage, ['div' , {'id':'body'}"])

我只得到div元素,body属性似乎被忽略了.

I get only the div element, the body attribute seems to get ignored.

有没有办法解决这个问题?

Is there a way to fix this?

推荐答案

def parse(html, *atrs):
 soup= BeautifulSoup(html)
 body = soup.find(*atrs)
 return body

然后:

parse(htmlpage, 'div', {'id':'body'})

这篇关于Python漂亮的汤参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆