我不能使用 Try-except

I can't use Try-except

我有一个要抓取的页面,但并不总是可以抓取 我希望代码为 运行 24/7 所以我做了这个

import requests
import response
from bs4 import BeautifulSoup
import re
import time
import io
import ssl
import os
import getpass
import urllib3
from time import sleep
from lxml.etree import tostring
import subprocess
import smtplib
import sys
import lxml
print('Enter the National-ID : ')
NID = input()
burp0_url = "I removed the URL"
burp0_cookies = {"I removed the cookies"}
burp0_headers = {"I removed the headers"}
burp0_data = "I removed the data"
r = requests.post(burp0_url, headers=burp0_headers, cookies=burp0_cookies, data=burp0_data)
soup=BeautifulSoup(r.content,'html.parser')
script = soup.find('script')
address = soup.find("input", {"name": "ctl00$ContentPlaceHolder1$EmpAdrs"})
DOB = soup.find("input", {"name": "ctl00$ContentPlaceHolder1$EmpBD"})
gender = soup.find('option', {'selected':'selected'})
status = soup.find('option', {'value':'206510000'})
for x in soup.findAll('table', {'class':'auto-style1'}):
    for no in soup.findAll('input', {'name':'ctl00$ContentPlaceHolder1$EmpNatNo'}):
        for name in soup.findAll('input', {'name':'ctl00$ContentPlaceHolder1$EmpName'}):
            with io.open('x.txt', 'a', encoding="utf-8") as f:
                ary = [name , no, address, DOB]      
                f.write (ary[0]["value"]+ ",")
                f.write (ary[1]["value"]+ ",")
                f.write (ary[2]["value"]+ ",")
                f.write (ary[3]["value"]+ ",")
                f.write (gender.text + ",")
                f.write (status.text + "\n")
                print (script) 

我的问题是当程序找不到(值)或任何它结束程序的时候 像这样

Traceback (most recent call last):
  File "C:\Users\Crybik\Desktop\jms\CORVID\Coding\pyoen.py", line 35, in <module>
    f.write (ary[0]["value"]+ ",")
  File "C:\python39\lib\site-packages\bs4\element.py", line 1406, in __getitem__
    return self.attrs[key]
KeyError: 'value'

我希望它继续执行 我试着做 try-except 的事情但我做不到而且没有用 而且我认为 Try-except 在我必须调试它们时不会显示错误。 那么有人可以帮助我吗?

循环打印你的字段,这样你就可以在每个字段周围放置 try/except

使用这个:

for x in soup.findAll('table', {'class':'auto-style1'}):
    for no in soup.findAll('input', {'name':'ctl00$ContentPlaceHolder1$EmpNatNo'}):
        for name in soup.findAll('input', {'name':'ctl00$ContentPlaceHolder1$EmpName'}):
            with io.open('x.txt', 'a', encoding="utf-8") as f:
                ary = [name , no, address, DOB]
                for field in ary:
                    try:
                        val = field["value"]
                    except:
                        val = ""
                    f.write(val + ",")
                f.write (gender.text + ",")
                f.write (status.text + "\n")
                print(script)

代替这个:

for x in soup.findAll('table', {'class':'auto-style1'}):
    for no in soup.findAll('input', {'name':'ctl00$ContentPlaceHolder1$EmpNatNo'}):
        for name in soup.findAll('input', {'name':'ctl00$ContentPlaceHolder1$EmpName'}):
            with io.open('x.txt', 'a', encoding="utf-8") as f:
                ary = [name , no, address, DOB]      
                f.write (ary[0]["value"]+ ",")
                f.write (ary[1]["value"]+ ",")
                f.write (ary[2]["value"]+ ",")
                f.write (ary[3]["value"]+ ",")
                f.write (gender.text + ",")
                f.write (status.text + "\n")
                print (script)