【问题标题】:Remove NoneType from BeautifulSoup从 BeautifulSoup 中删除 NoneType
【发布时间】:2018-11-15 04:44:37
【问题描述】:

我正在尝试从使用以下代码提取的数字中删除逗号:

with requests.Session() as s:
    url = 'https://www.zoopla.co.uk/for-sale/property/london/paddington/?q=Paddington%2C%20London&results_sort=newest_listings&search_source=home'
    r = s.get(url, headers=req_headers)
    soup = BeautifulSoup(r.content, 'lxml')
    prices = []
    for price in soup.find_all('a', {"class":"listing-results-price text-price"}):
        prices.append(price.text)
        if price is None:
            print('none')
    df['price'] = prices
    df['price'] = df['price'].str.extract('(\d+([\d,]?\d)*(\.\d+)?)', expand=True) #remove extract numbers with commas
    df['price'] = df['price'].replace(',','', inplace = True)

这将返回一列,其中所有值均为无。有没有办法消除这个 NoneType 错误?

在我运行最后一行之前,数据帧如下:

         price
0          NaN
1    1,875,000
2    4,950,000
3      500,000
4      675,000
5      980,000
6      475,000
7      849,950
8    1,050,000
9    1,050,000
10     650,000
11   1,100,000
12   1,300,000
13     895,000
14   1,000,000
15  26,800,000
16   1,600,000
17     695,000
18   2,100,000
19     510,000
20   1,200,000
21   3,000,000
22     599,000
23  26,800,000
24   1,550,000
25     750,000
26   1,600,000
27   1,025,000

【问题讨论】:

  • 你能告诉我们输入数据吗?
  • 我建议您将 if 条件提前一行。 :-)
  • @HarvIpan 在尝试替换最后一行的逗号之前,我已经编辑了帖子以显示数据。
  • @cwallenpoole 不幸的是,值仍被返回为 None
  • 使用 df['price'].replace(',','', inplace = True) ,您将替换 inplace,它不会返回任何内容。

标签: python pandas beautifulsoup nonetype


【解决方案1】:

使用 df['price'].replace(',','', inplace = True) ,您将替换 inplace,它不会返回任何内容。

你需要:

df['price'] = df['price'].str.replace(',','')

输出:

0        NaN
1    1875000
2    4950000
3     500000
4     675000
5     980000
6     475000
7     849950
8    1050000
9    1050000

供参考,请查看docs

【讨论】:

  • 谢谢。不幸的是,这只是返回与输入数据完全相同的列(带逗号的数字)。
  • @James,嗯,你确定用df['price'] = df['price'].str.replace(',','') 替换最后一行吗,注意.str 评估员。如果您不使用.str assesor,它将返回与您提到的相同的数据。
【解决方案2】:

我建议您应该在构建数据框之前在数据提取端对其进行处理,您可以按如下方式构建您的列表:

from bs4 import BeautifulSoup
import requests
url = 'https://www.zoopla.co.uk/for-sale/property/london/paddington/?q=Paddington%2C%20London&results_sort=newest_listings&search_source=home'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
res_lis = [int(price.text.strip().split('\n')[0].replace('£', '').replace(',', '')) for price in soup.find_all('a', {"class":"listing-results-price text-price"}) if price]
print(res_lis)

结果:

[2000000, 549950, 1050000, 500000, 675000, 980000, 475000, 849950, 1050000, 1050000, 650000, 1100000, 1300000, 895000, 1000000, 26800000, 1600000, 695000, 2100000, 510000, 3000000, 1200000, 599000, 26800000, 1550000, 750000, 1600000, 1025000]

如果您在存储数据之前尽可能地根据要求构造/操作所有数据,这将是您的数据提取阶段,然后

【讨论】:

    猜你喜欢
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多