【问题标题】:How to filter a text output?如何过滤文本输出?
【发布时间】:2019-08-29 20:33:01
【问题描述】:

我正在尝试从网页中获取一些数据,为此我使用了这个:

import bs4
import requests

r = requests.get("URLOFTHEWEBSITE")
soup = bs4.BeautifulSoup(r.text, 'lxml')

hi =soup.select('tbody')
hi0 = hi[0].getText()

print(hi0)

由此我得到输出:


**10 lines of nothing**

5522 
TRENTO
22:56

5

1

**10 lines of nothing**

Ferma a: PERGINE (23.01) - POVO MESIANO (23.11) - VILLAZZANO (23.15) - S.BARTOLAMEO (23.19) - S.CHIARA (23.21) - TRENTO (23.28)                                                                                                                                

*about 30 lines of nothing*

我只需要从“5522”到“1”的值,如下所示:

a = "5522"
b = "TRENTO"
c = "5"
d = "1"

如何过滤这些值并忽略所有其他文本?

我尝试使用filter() 命令,但没有成功。

我是 python 新手,所以如果这听起来像一个愚蠢的问题,我很抱歉:| .

【问题讨论】:

标签: python python-3.x beautifulsoup python-requests python-3.6


【解决方案1】:

如果一切都是相同的格式,我会尝试循环并设置一个标志来结束。

for line in hi0:
   if(line.contains('1')):
       #insert code here
       break #breaks you out of the loop

【讨论】:

    猜你喜欢
    • 2022-11-13
    • 2015-09-08
    • 2023-03-16
    • 2017-12-22
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多