下面出現的錯誤: 「if soup.find(text = bbb).parent.parent.get_text(strip = True AttributeError:'NoneType'object has沒有屬性「父」如何傳遞NoneTypes?所以爬蟲進行並不停止:
任何幫助將不勝感激,因爲我不能完全運行它,python只返回結果的錯誤,我需要它返回空,如果沒有項目,並繼續前進我試圖把一個IF語句,但是,這並不工作。
import csv
import re
import requests
from bs4 import BeautifulSoup
f = open('dataoutput.csv','w', newline= "")
writer = csv.writer(f)
def trade_spider(max_pages):
page = 1
while page <= max_pages:
url = 'http://www.zoopla.co.uk/for-sale/property/nottingham/?price_max=200000&identifier=nottingham&q=Nottingham&search_source=home&radius=0&pn=' + str(page) + '&page_size=100'
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for link in soup.findAll('a', {'class': 'listing-results-price text-price'}):
href = "http://www.zoopla.co.uk" + link.get('href')
title = link.string
get_single_item_data(href)
page += 1
def get_single_item_data(item_url):
source_code = requests.get(item_url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text)
for item_e in soup.findAll('table', {'class' : 'neither'}):
Sold = item_e.get_text(strip=True)
bbb = re.compile('First listed')
try:
next_s = soup.find(text=bbb).parent.parent.get_text(strip=True)
except:
Pass
try:
writer.writerow([ Sold, next_s])
except:
pass
trade_spider(2)
指定'result = soup.find(...)',然後在繼續訪問屬性之前檢查'if result:'。或者'try:'然後'catch AttributeError:'。或者使用'getattr'。 – jonrsharpe
謝謝,我很新的編碼,你能輸入你給我的代碼的例子,感謝您的幫助 – hello11
然後,我建議通過例如運行。 https://docs.python.org/3/tutorial/來掌握這個基本的語法。 – jonrsharpe