【问题标题】:Python - Scraped Text Results to a ListPython - 将文本结果抓取到列表中
【发布时间】:2018-11-27 06:35:04
【问题描述】:

我正在完成我的学术作业,并且不擅长 python,只是一个初学者。

我抓取了产品“名称”和“价格”。 问题:

1- 我想从输出中创建两个结果列表。使用 list() 命令无济于事,将每个名称分解为其字母,然后创建列表,这不是我想要实现的目标。

2- 有没有更好的方法从 ebay 上抓取产品名称及其价格,并在包含名称和相应价格以及规格特征(例如手机规格)的列表中获得结果?

感谢您的建议。 谢谢。

productName = list(productNameElement.find('h3').text)

这给出了:

【问题讨论】:

  • 请提供您的代码。现在还不完全清楚你在问什么,以及你到底在哪里卡住了代码。
  • 请与问题分享您的代码,以便我们为您提供帮助。
  • 你能分享一个网址/相关的html吗?
  • 另外,要非常清楚你所收集的信息是什么。规范特征可能相当广泛。同时分享您的代码,以便我们了解您当前使用的方法。

标签: python python-3.x web-scraping


【解决方案1】:

试试这个:

names = productNameElement.find('h3').text
names = names.split(" ")

【讨论】:

    【解决方案2】:

    试试这个方法,把它改成你的代码

    data=['a','p','p','l','e']
    list=("").join(data)
    print(list)
    

    输出:

    苹果

    【讨论】:

      【解决方案3】:

      为什么要重新发明轮子?最好使用BeautifulSoup 模块来正确处理 HTML 文件。

      类似:

      from bs4 import BeautifulSoup
      html_doc="<html>...Your html code ... <h3>myproductname_1</h3> still your html code to soup...<h3>myproductname_2</h3>...</html>"
      soup = BeautifulSoup(html_doc, 'html.parser')
      product_list=[]
      for productName in soup.find_all('h3'):
          print(productName.get_text())
          product_list.append(productName.get_text())
      print(product_list)
      

      给你:

      myproductname_1
      myproductname_2
      [u'myproductname_1', u'myproductname_2']
      

      【讨论】:

      • 'pip install beautifulsoup4' 来安装 Beautifulsoup
      猜你喜欢
      • 2015-05-03
      • 1970-01-01
      • 2021-03-16
      • 1970-01-01
      • 2016-03-02
      • 2022-01-06
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多