本文介绍了将参数传递给python中bs4中的findAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在函数中使用bs4的帮助.如果我想通过函数将路径传递给findAll(或find),它将无法正常工作.请参阅下面的示例.

I need help with using bs4 in a function. If I want to pass the path to findAll (or find) through function, it does not work. Please see the sample below.

from bs4 import BeautifulSoup
data = '<h1 class="headline">Willkommen!</h1>'

def check_text(path, value):

    soup = BeautifulSoup(''.join(data), "lxml")

    x1 = "h1", {"class":"headline"}
    x2 = path
    x3 = tuple(path)
    print type(x1), 'soup.findAll(x1)===', soup.findAll(x1)
    print type(x2), 'soup.findAll(x2)===', soup.findAll(x2)
    print type(x3), 'soup.findAll(x3)===', soup.findAll(x3)

    for i in soup.findAll(x1):
        print 'x1, text=', i.getText()

    for i in soup.findAll(x2):
        print 'x2, text=', i.getText()

    for i in soup.findAll(x3):
        print 'x3, text=', i.getText()


check_text('"h1", {"class": "headline"}', 'Willkommen!')

输出为

<type 'tuple'> soup.findAll(x1)=== [<h1 class="headline">Willkommen!     </h1>]

<type 'str'> soup.findAll(x2)=== []

<type 'tuple'> soup.findAll(x3)=== []

x1, text= Willkommen!

有人可以解决吗?谢谢

Does anyone have an solution?thanks

推荐答案

from bs4 import BeautifulSoup
data = '<h1 class="headline">Willkommen!</h1>'

def check_text(path, value):

    soup = BeautifulSoup(''.join(data), "lxml")

    x1 = "h1", {"class":"headline"}
    print (type(x1), 'soup.findAll(x1)===', soup.findAll(x1))
    print (type(path), 'soup.findAll(path)===', soup.findAll(**path))

    for i in soup.findAll(x1):
        print ('x1, text=', i.getText())

    for i in soup.findAll(**path):
        print ('path, text=', i.getText())


check_text({'name' : "h1", 'attrs': {"class": "headline"} }, 'Willkommen!')

传递一个字典,而不是传递一个字符串,该字典的元素可以作为关键字参数传递给被调用的函数.

instead of passing as a string, pass a dictionary, whose elements can be passed as keyword arguments to the called function.

这篇关于将参数传递给python中bs4中的findAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:23
查看更多