【发布时间】: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