2017-08-11 2 views
0

Je suis nouveau sur python. J'ai créé mon propre robot d'exploration Web qui est supposé racler Yelp pour la pratique.Web Crawler --- TypeError: coercing à Unicode: besoin de chaîne ou de tampon, NoneType trouvé


Je continue à obtenir cette erreur et ne peut pas sembler aller au-delà de la première page:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 26, in yelpSpider 
    TypeError: coercing to Unicode: need string or buffer, NoneType found 

Voici mon code:

import requests 
from BeautifulSoup import BeautifulSoup 
def yelpSpider(maxPages): 
    page = 0 
    listURL = [] 
    listRATE = [] 
    listAREA = [] 
    listADDRESS = [] 
    listType = [] 
    while page <= maxPages: 
     url = 'https://www.yelp.com/search?find_desc=Restaurants&find_loc=Manhattan,+NY&start=0' + str(page) 
     sourceCode = requests.get(url) 
     plainText = sourceCode.text 
     soup = BeautifulSoup(plainText) 
     for bizName in soup.findAll('a',{'class':'biz-name js-analytics-click'}): 
      href = 'https://www.yelp.com.com' + bizName.get('href') 
      listURL.append(href) 
     for rating in soup.findAll('img',{'class':'offscreen'}): 
      stars = rating.get('alt') 
      listRATE.append(stars) 
     for area in soup.findAll('span',{'class':'neighborhood-str-list'}): 
      listAREA.append(area.string) 
     for type in soup.findAll('span',{'class':'category-str-list'}): 
      listType.append(type) 
     for tracker in range(int(page),int(page) + 10): 
      print(listURL[tracker]) 
      print(' ') 
      print(listAREA[tracker] + ' | ' + listRATE[tracker]) 
     page += 10 

yelpSpider(20) 

Remerciez vous pour votre aide!

+0

Modifier la dernière impression à: ' print ('{} | {}'. format (listAREA [tracker], listRATE [tracker])) ' –

Répondre

0

Le problème se produit à print(listAREA[tracker] + ' | ' + listRATE[tracker])

Et ce qui se passe quand votre listRATE sort pour être

['4.5 star rating', 
'4.5 star rating', 
'4.5 star rating', 
'4.0 star rating', 
'4.0 star rating', 
'4.0 star rating', 
'4.0 star rating', 
'5.0 star rating', 
'4.5 star rating', 
'4.0 star rating', 
None, 
None, 
'4.0 star rating', 
'4.5 star rating', 
'4.0 star rating', 
'3.0 star rating', 
'4.0 star rating', 
'3.5 star rating', 
'4.5 star rating', 
'4.5 star rating', 
'5.0 star rating', 
'4.0 star rating', 
None, 
None] 

Comme vous pouvez voir l'index est tracker: 10 Aucun. Et aucun ne peut pas être utilisé dans la concaténation de chaîne.

Donc vous pouvez utiliser une condition or et la remplacer par ''. Votre code deviendra

print((listAREA[tracker] or '') + ' | ' + (listRATE[tracker] or '')) 

l'option suivante est de fixer votre listRATE avant impression

listRATE = list(map(lambda text: text if text is not None else 'N/A', listRATE)) 

Après execute le dessus de votre tableau va changer comme ci-dessous

['4.5 star rating', 
'4.5 star rating', 
'4.5 star rating', 
'4.0 star rating', 
'4.0 star rating', 
'4.0 star rating', 
'4.0 star rating', 
'5.0 star rating', 
'4.5 star rating', 
'4.0 star rating', 
'N/A', 
'N/A', 
'4.0 star rating', 
'4.5 star rating', 
'4.0 star rating', 
'3.0 star rating', 
'4.0 star rating', 
'3.5 star rating', 
'4.5 star rating', 
'4.5 star rating', 
'5.0 star rating', 
'4.0 star rating', 
'N/A', 
'N/A']