我有一个页面想要抓取,但并不总是可以抓取 我希望代码 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\cr\Desktop\jms\CORVID\Coding\py\1oen.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)