mercredi 29 septembre 2021

How to loop through all subject names if subject name contains special Characters that are not matching in soup

  1. This code generates attribute Error because it doesn't match with subjects in web page due to special characters in Subject name.
  2. Provide any solution for the following code if subject changes
import requests
from bs4 import BeautifulSoup
import csv

seat = ""
total = ""
remarks = ""
percentage = ""
subjects = ["MANAGEMENT","PROGRAMMING WITH PYTHON","MOBILE APPLICATION DEVELOPMENT","EMERGING TRENDS IN COMPUTER AND INFORMATION TECHNOLGY","NETWORK AND INFORMATION SECURITY","ENTERPRENURESHIP DEVELOPMENT","CAPSTONE PROJECT EXECUTION & REPORT WRITTING"]

marks = []
start = 302060
end = 302065
for seat_number in range(start,end):
    URL = "https://msbte.org.in/DISRESLIVE2021CRSLDSEP/COV6139QS21LIVEResult/SeatNumber/30/"+str(seat_number)+"Marksheet.html"
    r = requests.get(URL)
    soup = BeautifulSoup(r.content,'html.parser')
    
    name=soup.find_all('table')[0].find_all('tr')[0].find_all('td')[1].text

    seat = soup.find('td',text="SEAT NO.").find_next('td').text
    marks.append(seat)
    #name_of Student
    marks.append(name)
  1. Below part of code generates error it does not works for all the subjects
    for subject in subjects:
        var=soup.find('td',text=subject)
        for i in range(36):
            if subject=="PROGRAMMING WITH PYTHON" or subject=="MOBILE APPLICATION DEVELOPMENT" or subject=="NETWORK AND INFORMATION SECURITY":
                if(i==5):
                    marks.append(var.text)
                if(i==14):
                    marks.append(var.text)
                if(i==23):
                    marks.append(var.text)
                if(i==32):
                    marks.append(var.text)
                var = var.find_next('td')
            else:
                if(i==5):
                    marks.append(var.text)
                if(i==14):
                    marks.append(var.text)
                var = var.find_next('td')

    #for getting total,percentage,remarks
    total = soup.find_all('table')[2].find_all('tr')[1].find_all('td')[2].text
    percentage = soup.find_all('table')[2].find_all('tr')[1].find_all('td')[1].text
    remarks = soup.find_all('table')[2].find_all('tr')[2].find_all('td')[1].text
    sub=soup.find_all('table')[1].find_all('tr')[2].find_all('td')[0].text
    #print(sub)
#    sub
    marks.append(total)
    marks.append(percentage)
    marks.append(remarks)
    #add to the excel sheet
    with open("C:\\sem6.csv", 'a', newline='') as f:
        w = csv.writer(f)
        w.writerow(marks)
    del marks[:]



Aucun commentaire:

Enregistrer un commentaire