UnicodeCodeError in BeautifulSoup WebcraperPython

Python-Programme
Anonymous
 UnicodeCodeError in BeautifulSoup Webcraper

Post by Anonymous »

Ich habe einen Unicode -Encodierfehler mit dem folgenden Code für einen einfachen Webschaber. < /p>

Code: Select all

print 'JSON scraper initializing'

from bs4 import BeautifulSoup
import json
import requests
import geocoder

# Set page variable
page = 'https://www.bandsintown.com/?came_from=257&page='
urlBucket = []
for i in range (1,3):
uniqueUrl = page + str(i)
urlBucket.append(uniqueUrl)

# Build response container
responseBucket = []

for i in urlBucket:
uniqueResponse = requests.get(i)
responseBucket.append(uniqueResponse)

# Build soup container
soupBucket = []
for i in responseBucket:
individualSoup = BeautifulSoup(i.text, 'html.parser')
soupBucket.append(individualSoup)

# Build events container
allSanFranciscoEvents = []
for i in soupBucket:
script = i.find_all("script")[4]

eventsJSON = json.loads(script.text)

allSanFranciscoEvents.append(eventsJSON)

with open("allSanFranciscoEvents.json", "w") as writeJSON:
json.dump(allSanFranciscoEvents, writeJSON, ensure_ascii=False)
print ('end')
< /code>
Die seltsame Sache ist manchmal, dass dieser Code funktioniert und keinen Fehler gibt.  Es hat mit der für i in Bereich 
Zeile des Codes zu tun. Wenn ich beispielsweise (2,4) für den Bereich eingebaut habe, funktioniert es einwandfrei. Wenn ich es auf 1,3 ändere, ist es :

Code: Select all

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 12: ordinal not in range(128)
< /code>
Wie kann ich dieses [url=viewtopic.php?t=15738]Problem[/url] in meinem Code beheben?  Wenn ich Allsanfranciscoevents 
drucke, wird in allen Daten gelesen, sodass ich glaube, dass das Problem im letzten Code mit dem JSON -Dump stattfindet.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post