Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.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 美化组解析表并筛选第二行_Python_Html_Parsing_Beautifulsoup - Fatal编程技术网

Python 美化组解析表并筛选第二行

Python 美化组解析表并筛选第二行,python,html,parsing,beautifulsoup,Python,Html,Parsing,Beautifulsoup,我跟随,想在br之后过滤所有内容 以下是一个例子: <td class="flightAirport first">Palma de Mallorca<br><span class="second_row">nach Berlin Tegel</span></td> Palma de Mallorcanach Berlin Tegel 我得到的是:>柏林马洛卡那棕榈树酒店 p我试过什么:br

我跟随,想在br之后过滤所有内容

以下是一个例子:

<td class="flightAirport first">Palma de Mallorca<br><span class="second_row">nach Berlin Tegel</span></td>
Palma de Mallorca
nach Berlin Tegel
我得到的是:>柏林马洛卡那棕榈树酒店 p我试过什么:br
从字符串中剥离“nach Berlin Tegel”,会得到一个缺少字符的字符串,如>Palma de MallorBeautifulSoup的文本函数包括一个名为stripped_strings的函数,该函数返回在汤中找到的所有字符串的生成器。该生成器的第一个字符串将是br标记之前的文本。更改引用的代码并使用它分析示例html:

from bs4 import BeautifulSoup

sample_table = """
<table>
  <tr>
    <td class="flightAirport first">Palma de Mallorca<br><span class="second_row">nach Berlin Tegel</span></td>
    <td class="flightAirport first">LAX</td>
    <td class="flightAirport first"></td>
  </tr>
</table>"""

data = []
soup = BeautifulSoup(sample_table, 'html.parser')
table = soup.find("table")
for row in table.findAll("tr"):
    cols = [ e.stripped_strings.next() for e in row.find_all('td') if len(e.text)]
    data.append([e for e in cols if e])

print(data)
从bs4导入美化组
样本_表=”“
马洛卡棕榈酒店
松懈的
"""
数据=[]
soup=BeautifulSoup(示例_表'html.parser')
table=soup.find(“table”)
对于表.findAll(“tr”)中的行:
cols=[e.stripped_strings.next()表示行中的e.find_all('td'),如果len(e.text)]
data.append([e表示列中的e,如果是e])
打印(数据)