【问题标题】:Python3 - ValueError: not enough values to unpack (expected 3, got 2)Python3 - ValueError:没有足够的值来解包(预期 3,得到 2)
【发布时间】:2019-01-05 23:15:29
【问题描述】:

我正在尝试做一些实验性的网络抓取,并询问在以下情况下是否有可能克服 ValueError。作为示例,以下 5 个数据字段是我想要进行网络抓取的:

1) Car Model: Honda Fit Auto 1.3
2) Price: S$19,000
3) Date post: 3 weeks ago by back_packer
4) Depreciation: S$8,362.75
5) Registration Date: 15 Jan 2010

从网站的html中,2)到5)的数据在同一个标​​签下

<p class="cU-b cU-d">3 weeks ago by <a href="/back_packer" rel="nofollow " 
target="_blank">back_packer</a></p>
<p class="cU-b cU-d">S$19,000</p>
<p class="cU-b cU-d">S$8,362.75</p>
<p class="cU-b cU-d">15 Jan 2010</p>

因此,我尝试运行以下 Python 代码。

def getHTML(link, counter):
    return bs(get(link.format(counter)).content, "html.parser")

PAGE_URL = 'https://sg.carousell.com/categories/cars-32/cars-for-sale-1173/'
CAR_URL = 'https://sg.carousell.com/p/{}'

car = dict()
content = getHTML(CAR_URL, car_id).find('div', {'class': 'aG-c aG-b'})
car['Model'] = content.find('p', {'class': 'cU-b cU-e'}).text

car['Post'], car['Price'], car['Deprec'], car['Regstr_Date'] = {info.text for 
info in content.find_all('p', {'class': 'cU-b cU-d'})}

========================================

当我尝试运行时,我会遇到“ValueError: no enough values to unpack (expected 3, got 2)”。我怀疑该错误是由于至少有一项汽车记录缺少邮寄、价格、折旧或注册日期的字段。

提前致谢。

【问题讨论】:

  • 您可以编辑您的问题并发布示例 html 代码/网址和所需的输出吗?
  • 您好 Andrej,感谢您的反馈。我试图编辑我的问题。希望现在更清楚了。

标签: python-3.x


【解决方案1】:

该页面不是很友好,因此您可以使用试错法,直到您得到正确的结果。我的尝试就在这里(我用缺失值替换了"-",至少它不会抛出 ValueError 但你需要检查它是否抓取了正确的信息):

from bs4 import BeautifulSoup as bs
from requests import get
import re
from pprint import pprint


def getHTML(link, counter):
    return bs(get(link.format(counter)).content, "html.parser")

PAGE_URL = 'https://sg.carousell.com/categories/cars-32/cars-for-sale-1173/'
CAR_URL = 'https://sg.carousell.com/p/{}'

# car_id = 'mazda-3-sedan-auto-1-5-182030279'
car_id = 'nissan-nv200-1-5-manual-182141686'
# car_id = 'toyota-corolla-axio-1-5-auto-x-177344405'

car = {}
content = getHTML(CAR_URL, car_id).find('div', {'class': 'aG-c aG-b'})
car['Model'] = content.find('p', {'class': 'cU-b cU-e'}).text

data = []
for p in content.select('section.bi-c.bi-h p.cU-b.cU-d')[:4]:
    if re.match(r'\d+\s+Likes', p.text):
        break
    data.append(p.text)

car['Post'], car['Price'], car['Deprec'], car['Regstr_Date'], *_ = data + ['-'] * 4

# swap Deprec and Registration Date?
if car['Deprec'] != '-' and '$' not in car['Deprec']:
    car['Regstr_Date'], car['Deprec'] = car['Deprec'], car['Regstr_Date']

pprint(car)

这辆车的印刷品:

{'Deprec': '-',
 'Model': 'Nissan NV200 1.5 Manual',
 'Post': 'an hour ago by rubberr',
 'Price': 'S$25,800',
 'Regstr_Date': '-'}

【讨论】:

  • 感谢您的帮助!真的很感激^^
猜你喜欢
  • 2019-02-08
  • 2017-09-14
  • 2017-07-04
  • 2020-07-17
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多