首页 > 解决方案 > Python - 从字符串中过滤数据 - 在逗号之前,中间和之后

问题描述

我对编程很陌生,我开始学习 Python。

对于一个小的网络抓取项目,我正在使用通过 Xpath 收集以下数据作为字符串的代码。

目标是获取此数据并将平方英尺转换为平方米。

在此处输入图像描述

对于此示例,代码返回以下字符串:“47′-4″ 宽,82′ 深,26′ 高”

这是代码:

import requests
from lxml import html
import re

resp = requests.get(
                    url="https://tyreehouseplans.com/shop/bedrooms/5-bedrooms/avenue/",
                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
)

tree = html.fromstring(html=resp.text)

dimensions = tree.xpath("//h3[contains(., 'House Size')]/following-sibling::p/text()")

dimensions = ','.join(dimensions)

dimensions = dimensions.replace('\n', '')

print(dimensions)

看着这个字符串:“47′-4″ 宽,82′ 深,26′ 高” 我认为进行转换的最佳方法是将数据过滤成三部分:第一个逗号之前的数据(47-4),然后第一个和第二个逗号之间的数据 (82),最后是第二个逗号之后的数据 (26)。

我想如果我能得到这 3 个部分并将其存储在三个变量中,我可以测试变量以检查格式何时不同,例如,如果变量包含 hifen (47-4),则脚本将获取第一个数字以英尺为单位,秒为英寸,因此我可以将它们都转换为米,求和,最后将这些数字转换为平方米。

如果我测试变量并且不包含 hifen,我只需将英尺转换为米,就是这样 =)

小伙伴们,你们觉得我的想法好吗?

如果是,如何划分/获取该字符串的这三个部分?

我想使用逗号作为“模式”将是使用 IF 进行分隔、测试和处理的最佳方式......

你们能帮帮我吗,伙计们?

标签: pythonstringweb-scraping

解决方案


您在一个字符串中包含逗号和值的原因是您在网络抓取返回时调用了 .join() 。

返回实际上是一个列表,你可以使用下面的方法来提取值(无论格式如何),只要它们每次的顺序相同即可;宽、深、高。(See below for method of detecting regardless of order)

import requests
from lxml import html
import re

resp = requests.get(
                    url="https://tyreehouseplans.com/shop/bedrooms/5-bedrooms/avenue/",
                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
)

tree = html.fromstring(html=resp.text)

dimensions = tree.xpath("//h3[contains(., 'House Size')]/following-sibling::p/text()")

wide = dimensions[0].replace('\n', '').split(' ')[0]
deep = dimensions[1].replace('\n', '').split(' ')[0]
high = dimensions[2].replace('\n', '').split(' ')[0]


print(wide)
print(deep)
print(high)

这返回;

47′-4″
82′
26′
>>> 

然后,您将能够执行每个测量的格式检查。

编辑:

如果测量的位置是可变的或变化的。下面的代码将不管顺序检测测量;

import requests
from lxml import html
import re

resp = requests.get(
                    url="https://tyreehouseplans.com/shop/bedrooms/5-bedrooms/avenue/",
                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Appl eWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
)

tree = html.fromstring(html=resp.text)

dimensions = tree.xpath("//h3[contains(., 'House Size')]/following-sibling::p/text()")


wide = [v for v in dimensions if 'wide' in v]
deep = [v for v in dimensions if 'deep' in v]
high = [v for v in dimensions if 'high' in v]

wide = wide[0].replace('\n', '').split(' ')[0]
deep = deep[0].replace('\n', '').split(' ')[0]
high = high[0].replace('\n', '').split(' ')[0]


print(wide)
print(deep)
print(high)

编辑:

您可以将上面的代码进一步压缩为每个维度的漂亮的小衬里。

wide = [v.replace('\n', '').split(' ')[0] for v in dimensions if 'wide' in v][0]
deep = [v.replace('\n', '').split(' ')[0] for v in dimensions if 'deep' in v][0]
high = [v.replace('\n', '').split(' ')[0] for v in dimensions if 'high' in v][0]

print(wide)
print(deep)
print(high)

推荐阅读