오류 상황
- 크롤링하면서 script를 json으로 변경하는 곳에서 아래와 같은 오류 발생함.
script_json = json.loads(script.string)
- 에러 메세지
AttributeError: 'NoneType' object has no attribute 'string'
- 그러나 script를 확인해보니 내용이 존재함.
해결방법
- script에는 내용이 존재하니, 이거를 직접 string으로 변환해야 함.
if script is not None:
script_content = script.string
if script_content is None:
script_content = ''.join(script.contents)
try:
script_json = json.loads(script_content.strip())
print(script_json)
except json.JSONDecodeError as e:
print("JSON 디코딩 에러:", e)
- 이렇게 변경하니 json으로 변환이 되었으며, 적재가 잘되는 것을 알 수 있음.