简体   繁体   中英

Skip Over Item if element doesn't exist on page when looping through multiple pages - BeautifulSoup and Python

I have a script that loops through multiple pages of a website and I want to skip over or add a blank space for the item that might not be on certain pages. For example, there are some pages that do not contain a license. When I run into one of those pages I get an attribute error. My script below loops through the first two pages with no problem, but when it hits the third page it stops. How can I fix this? Here is my script:

from urllib.request import urlopen
from bs4 import BeautifulSoup as soup
import json

base_url = "https://open.umn.edu/opentextbooks/"

data = []
n = 50
for i in range(4, n+1):
   response = urlopen(base_url + "BookDetail.aspx?bookId=" + str(i))
   page_html = response.read()
   response.close()

   #html parsing
   page_soup = soup(page_html, "html.parser")

   #grabs info for each textbook
   containers = page_soup.findAll("div",{"class":"LongDescription"})
   author = page_soup.select("p")

   for container in containers:
       item = {}
       item['type'] = "Textbook"
       item['title'] = container.find("div",{"class":"twothird"}).h1.text
       item['author'] = author[3].get_text(separator=', ')
       if item['author'] == " ":
          item['author'] = "University of Minnesota Libraries Publishing"
       item['link'] = "https://open.umn.edu/opentextbooks/BookDetail.aspx?bookId=" + str(i)
       item['source'] = "Open Textbook Library"
       item['base_url'] = "https://open.umn.edu/opentextbooks/"
       item['license'] = container.find("p",{"class":"Badge-Condition"}).a.text
       if item['license'] != container.find("p",{"class":"Badge-Condition"}).a.text:
          item['license'] = ""
       item['license_url'] = container.find("p",{"class":"Badge-Condition"}).a["href"]
       data.append(item) # add the item to the list

   with open("./json/noSubject/otl-loop.json", "w") as writeJSON:
      json.dump(data, writeJSON, ensure_ascii=False)

I figured it out. My main issue was with item['license'] Here is my fix:

if container.find("p",{"class":"Badge-Condition"}).a:
        item['license'] = container.find("p",{"class":"Badge-Condition"}).a.text
if container.find("img",{"class":"ctl00_maincontent_imgLicence"}):
        item['license'] = ''
if container.find("p",{"class":"Badge-Condition"}).a:
        item['license_url'] = container.find("p",{"class":"Badge-Condition"}).a["href"]
if container.find("img",{"class":"ctl00_maincontent_imgLicence"}):
        item['license_url'] = ''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM