【问题标题】:Scrape data sorting by conditional list按条件列表刮取数据排序
【发布时间】:2020-11-12 19:07:45
【问题描述】:

我正在从网络上抓取数据,并尝试使用 BRAND、MODEL、PN...等来构建这些数据。

阅读How to extract brand from product name 并发现无法通过计算机完成所有操作,因此我决定同时使用人和计算机。

原始数据示例:['NOKIA 5.3 phone (black)', 'NOKIA 5.3 phone (white)', 'NOKIA 5.3 phone']

def textbetween(text):
     s = text
     start = '('
     end = ')'
     s_int = s.find(start,3)
     s_end = s.find(end,3)+1
     found = s[s_int:s_end]
     return found
def textcheck(TEXT):
     spec_temp = textbetween(TEXT)
     remove_spec = TEXT.replace(spec_temp, '')
     check = remove_spec.split(' ')
     return check

在上面之后我会得到一个单词列表并使用 for 循环从原始数据中读取它[1, 2, 3...]

我试图创建一些空列表并使用条件来

raw = ['NOKIA 5.3 phone (black)', 'NOKIA 5.3 phone  (white)', 'NOKIA 5.3 phone ']
BRAND = []
MODEL = []
PN = []
OTHER = []
for i in raw:
   check = textcheck(i)
   for temp in check:
      while temp not in BRAND or MODEL or PN:
      print(temp, '<=Brand? leave blank if no, or type anything as Yes')
      human= input()
      if human!= '':
          print('this is a brand, add to brand list')
          BRAND.append(temp)
          break
      elif human == '':
          print(temp, '<=Model? leave blank if no, or type anything as Yes')
          human= input()
          if human!= '':
              print('this is a model, add to model list')
              MODEL.append(temp)
              break
          elif human== '':
              print(temp, '<=PN? leave blank if no, or type anything as Yes')
              human= input()
              if human!= '':
                  print('this is a PN, add to PN list')
                  PN.append(temp)
                  break
              else:
                 print('Do nothing')
                 break
           else:
              pass
       else:
         pass

      

在上述条件下,我应该得到一个 BRAND、MODEL、PN 的列表 - 人类第一次定义,机器检查单词是否在 BRAND 列表中,任何原始数据都可以流式传输到这个结构。 到目前为止,定义空列表不起作用 - 在我运行第一个产品名称之后,品牌已经有了诺基亚,但第二次机器再次询问我哪个诺基亚已经在列表中。因为列表是基于输入的原始数据和人工定义的动态列表,我如何运行它并使条件适用于对数据进行排序?

并计划导出列表excel之类的

  1. 原始数据:NOKIA 5.3 手机(黑色) 品牌:NOKIA 型号:5.3 PN:--
  2. 品牌列表(存储以备后用)
  3. 型号列表(存储以备后用)

Expect Output Like this

RAW 数据更新 https://drive.google.com/drive/folders/1SHLKqOLjL5wJpkEoC_C_peD3Ogwt8tVS?usp=sharing

【问题讨论】:

  • 你期望输出什么??
  • 品牌、型号、PN、其他 => 表现出色。主要是为了确保原始数据可以按列表的条件进行排序。
  • @wiskinglin 发布“原始数据示例”的预期输出
  • @更木健八。在excel中更新为这样的图片。谢谢
  • @wiskinglin 发布一些包含 PN 和不同品牌的示例数据

标签: python list conditional-statements


【解决方案1】:

试试这个 =^..^= 这将解析字符串并将数据拆分为所需的变量。

import pandas as pd

df = pd.read_excel('Example_RAW.xlsx')
df_results = pd.DataFrame()

for index, row in df.iterrows():
    parts = row[0].split('(')

    data_list = parts[0].split(' ')
    data_list = list(filter(None, data_list))

    # get brand
    brand = data_list[0]
    # get model
    model =' '.join([x for x in data_list if len(x) < 9][1:])
    # get pn
    pn_find = [x for x in data_list if len(x) > 9]
    if len(pn_find) > 0:
        pn = pn_find[0]
    else:
        pn = ''
    # get opther
    if len(parts) == 2:
        other = parts[1].replace(')', '')
    else:
        other = ''

    # collect data
    collected_data = {'brand': brand, 'model': model, 'pn': pn, 'other': other}
    data = pd.Series(collected_data)
    df_results = df_results.append(data, ignore_index=True)

df_results.to_csv('collected_data.csv')

输出:

  brand       model                                            other                   pn
0  ASUS                            i5-10210U/8G/512G PCIe/W10/FHD/14   X420FA-0108S10210U
1  ASUS  ZenBook 14                i7-1065G7/8G/512G PCIe/W10/FHD/14  UX425JA-0052G1065G7
2    HP                          14/i5-1035 G1/8G/512GB PCIe/W10/FHD         14s-dq1009TU
3    HP                            i5-10210U/8G/512G PCIe/W10/FHD/14         14s-cf2006TU
4  ASUS              i5-1035G1/4G/MX110-2G/1T+256G PCIe/W10/FHD/15.6   X509JB-0111S1035G1

【讨论】:

  • 谢谢,我会将它用作一些字符串数据的函数。我仍在使用我的设计,并将对其进行更新。
猜你喜欢
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-02
  • 2021-09-12
相关资源
最近更新 更多