Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从BeautifulSoup中的find_all结果中排除标记_Python_Beautifulsoup - Fatal编程技术网

Python 如何从BeautifulSoup中的find_all结果中排除标记

Python 如何从BeautifulSoup中的find_all结果中排除标记,python,beautifulsoup,Python,Beautifulsoup,我正在尝试美丽的汤,并使用下面的代码提取一些数据 response = requests.get(url_fii, headers=headers) response.encoding = 'utf-8' soup = BeautifulSoup(response.text,'html.parser') print (soup) 我得到以下输出: <table class="holiday_list" width="100%"> <tr> <th colspan

我正在尝试美丽的汤,并使用下面的代码提取一些数据

response = requests.get(url_fii, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text,'html.parser')

print (soup)
我得到以下输出:

<table class="holiday_list" width="100%">
<tr>
<th colspan="5" style="text-align:center">Heading line</th>
</tr>
<tr>
<th style="text-align:center">Category</th>
<th style="text-align:center">Date</th>
<th style="text-align:center">Value 1</th>
<th style="text-align:center">Value 2</th>
<th style="text-align:center">Value 3</th>
</tr>
<tr class="alt">
<td class="first"> Quantity</td>
<td class="date">09-Apr-2020</td>
<td class="number">7277.03</td>
<td class="number">5539.41</td>
<td class="number">1737.62</td>
</tr>
</table>
输出:

Heading line


Category
Date
Value 1
Value 2
Value 3


 Quantity
09-Apr-2020
7277.03
5539.41
1737.62

唯一不需要的部分是“航向线”。由于它也包含在中,因此它也会出现在输出中。但是,我注意到它有一个额外的属性,即“colspan”。如何将其用作过滤器,使“标题行”不会显示在输出中。

可以使用如下切片表示法跳过数组的第一个元素:

for p in soup('tr')[1:]:
    print(p.text)

有关切片表示法的更多信息,请参见此页。

可以使用如下切片表示法跳过数组的第一个元素:

for p in soup('tr')[1:]:
    print(p.text)

有关切片表示法的更多信息,请参见此部分。

基于上述答案,以下是我现在使用的代码:

response = requests.get(url_fii, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text,'lxml')

for p in soup('tr')[1:]:
    binNames = p.find_all('th')
    binValues = p.find_all('td')
    nBins = 0
    nValues = 0

    #The below section is for calculating the size of binNames. I didn't know of a better way than this.    
    for i in binNames:
            if len(i) > 0:
                nBins += 1

    #Now we print the binNames
    if nBins > 0:
        for i in range(nBins):
            print(binNames[i].text)

    #The below section is for calculating the size of binValues. I didn't know of a better way than this. 
    for i in binValues:
            if len(i) > 0:
                nValues += 1

    #Now we print the binValues
    if nValues > 0:
        for i in range(nValues):
            print(binValues[i].text)

根据以上答案,以下是我现在使用的代码:

response = requests.get(url_fii, headers=headers)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text,'lxml')

for p in soup('tr')[1:]:
    binNames = p.find_all('th')
    binValues = p.find_all('td')
    nBins = 0
    nValues = 0

    #The below section is for calculating the size of binNames. I didn't know of a better way than this.    
    for i in binNames:
            if len(i) > 0:
                nBins += 1

    #Now we print the binNames
    if nBins > 0:
        for i in range(nBins):
            print(binNames[i].text)

    #The below section is for calculating the size of binValues. I didn't know of a better way than this. 
    for i in binValues:
            if len(i) > 0:
                nValues += 1

    #Now we print the binValues
    if nValues > 0:
        for i in range(nValues):
            print(binValues[i].text)

将“tr”改为“td”不是更好吗
对于汤中的o('td'):
您可以简单地将
用于汤中的p('tr')[1://code>@Junitar您的建议奏效了。谢谢。如果你能在答案中解释会更好。@r-初学者如果我同意你的建议,我想要的一些输出将被忽略。将“tr”改为“td”不是更好吗
对于汤中的o('td'):
您可以简单地将
用于汤中的p('tr')[1://code>@Junitar您的建议奏效了。谢谢。如果你能在答案中解释就更好了。@r-初学者如果我同意你的建议,我想要的一些输出将被忽略。请看一下我的代码。你可能对此有一些意见。请看一下我的代码。你可能对此有一些评论。