下面出现的错误: “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