1 回答

TA貢獻1806條經驗 獲得超5個贊
現在,通過執行日志我可以告訴你蜘蛛中有兩個問題,似乎都與start_urls.
第一個例外:
File "C:\Users\Jatencio\PycharmProjects\testy\testdigi\testdigi\spiders\digike.py", line 93, in parse
'Quantity': cleaned_quantity,
UnboundLocalError: local variable 'cleaned_quantity' referenced before assignment
您在定義它之前引用了它cleaned_quantity。問題在這里:
if p.css('td.tr-minQty.ptable-param span.desktop::text').get():
quantity = p.css('td.tr-minQty.ptable-param span.desktop::text').get()
quantity = quantity.strip()
cleaned_quantity = int(quantity.replace(',', ''))
else:
quantity = 'No quantity'
如果您的 if 語句解析為 false,則永遠不會定義 cleaned_quantity,并且會在您嘗試組裝您的項目時引發錯誤:
yield {
'Part': parts1,
'Quantity': cleaned_quantity,
'Price': cleaned_price,
'Stock': cleaned_stock,
'Status': cleaned_status,
}
這只發生在幾次迭代中,而不是全部。
第二個例外:
File "C:\Users\Jatencio\PycharmProjects\testy\testdigi\testdigi\spiders\digike.py", line 55, in parse
p.css('td.tr-mfgPartNumber span::text').remove()
[...]
File "c:\users\jatencio\pycharmprojects\testy\venv\lib\site-packages\parsel\selector.py", line 371, in remove
raise CannotRemoveElementWithoutRoot(
parsel.selector.CannotRemoveElementWithoutRoot: The node you're trying to remove has no root, are you trying to remove a pseudo-element? Try to use 'li' as a selector instead of 'li::text' or '//li' instead of '//li/text()', for example.
這里的問題是你.remove()在 parsel 調用偽元素的方法中使用方法,你只能用來從 HTML 樹中刪除實際元素,所以我相信這應該可以解決問題:
改變這個:
p.css('td.tr-mfgPartNumber span::text').remove()
對此:
p.css('td.tr-mfgPartNumber span').remove()
您使用該方法的所有行都是這種情況remove。
如果這解決了您的問題,請告訴我。
添加回答
舉報