I'm getting KeyError trying to scrape data fro

2020-05-09 22:00发布

问题:

I wrote a code for data scraping; it works well for some pages, but for some it displays:

KeyError: 'isbn'.

Could you please guide me on how can I solve this issue?

Here is my code:

import requests
import re
import json
from bs4 import BeautifulSoup
import csv
import sys
import codecs


def Soup(content):
    soup = BeautifulSoup(content, 'html.parser')
    return soup


def Main(url):
    r = requests.get(url)
    soup = Soup(r.content)
    scripts = soup.findAll("script", type="application/ld+json",
                           text=re.compile("data"))
    prices = [span.text for span in soup.select(
        "p.product-field.price span span") if span.text != "USD"]
    with open("AudioBook/Fiction & Literature/African American.csv", 'a', encoding="utf-8", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Title", "Writer", "Price", "IMG", "URL", "ISBN"])
        for script, price in zip(scripts, prices):
            script = json.loads(script.text)
            title = script["data"]["name"]
            author = script["data"]["author"][0]["name"]    
            img = f'https:{script["data"]["thumbnailUrl"]}'
            isbn = script["data"]["isbn"]
            url = script["data"]["url"]
            writer.writerow([title, author, price, img, url, isbn])
for x in range(1,10):
    url = ("https://www.kobo.com/ww/en/audiobooks/contemporary-1?pageNumber=" + str(x))
    print("Scrapin page " + str(x) + ".....")
    Main(url)

回答1:

Since audiobooks don't have an ISBN on the listings page, you could prepare for this case with a default value, e.g.:

isbn = script["data"].get("isbn", "")

In this case, if the "isbn" key doesn't exist in script["data"], it will fallback on the value of an empty string.

Alternatively, you could get the book ISBN from the audiobook-specific page (your script["data"]["url"] above), e.g.:

def Main(url):
    r = requests.get(url)
    soup = Soup(r.content)
    scripts = soup.findAll("script", type="application/ld+json",
                           text=re.compile("data"))
    prices = [span.text for span in soup.select(
        "p.product-field.price span span") if span.text != "USD"]
    with open("AudioBook/Fiction & Literature/African American.csv", 'a', encoding="utf-8", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Title", "Writer", "Price", "IMG", "URL", "ISBN"])
        for script, price in zip(scripts, prices):
            script = json.loads(script.text)
            title = script["data"]["name"]
            author = script["data"]["author"][0]["name"]    
            img = f'https:{script["data"]["thumbnailUrl"]}'
            # NEW CODE
            url = script["data"]["url"] 
            if "isbn" in script["data"]:
                # ebook listings
                isbn = script["data"]["isbn"]
            else:
                # audiobook listings                            
                r = requests.get(url)                                               
                inner_soup = Soup(r.content)
                try:                                       
                    inner_script = json.loads(
                        inner_soup.find("script", type="application/ld+json",                           
                                        text=re.compile("workExample")).text)                           
                    isbn = inner_script["workExample"]["isbn"]
                except AttributeError:
                    isbn = ""
            # END NEW CODE
            writer.writerow([title, author, price, img, url, isbn])