다양한 분야 공부

파이썬 혼자놀기 패키지 - 2일차 _ 기사 웹크롤링

Conpresent 2020. 10. 11. 16:23

news.py 파이썬 파일 만들기

 

//

 

 

 

코딩은 계속 실행하면서 맞춰가는 것임

한번에 딱 정답을 쓰고 보는 것이 아님

최종본

이것을 엑셀에 바로 저장하기

 

새로운 패키지를 깔았을때 test 해보기 위해서 temp.py 파일을 만들어줌

 

Run 하면

 

두개를 합쳐보기

 

이메일 보내기

 

 

 

 

1. 이메일주소/비밀번호가 틀린경우

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\\n5.7.8 <https://support.google.com/mail/?p=BadCredentials> h9sm3842285pfc.28 - gsmtp')

2. 2-step verification이 켜져있는 경우

smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\\n5.7.9 <https://support.google.com/mail/?p=InvalidSecondFactor> n7sm4135021pfq.114 - gsmtp')

  1. '보안 수준이 낮은 앱의 액세스'가 사용 중지된 경우

    smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\\n5.7.8 <https://support.google.com/mail/?p=BadCredentials> r3sm4185855pfh.88 - gsmtp')

    이메일 주소 / 비밀번호를 틀렸을 때와 같은 에러 메시지가 나오는데, 링크를 따라가보시면 다른 이메일 플랫폼을 통해 Gmail에 로그인할 수 없을 때의 해결법이 나옵니다. 여기서 '보안 수준이 낮은 앱이 계정에 액세스하도록 허용'을 따라가서 풀어주시면 됩니다.

여러사람에게 각각 이메일 보내기

반복문을 이용하면 쉽게 여러 사람에게 이메일을 보낼 수 있습니다. 받는 사람 주소를 리스트에 넣어 반복문을 돌려봅시다.

 

파일첨부하기

  • 파일 첨부하기
    • 메일에 파일을 첨부하기 위해서는 파일 내용을 컴퓨터가 이해할 수 있는 이진수로 바꿔주어야합니다. 여러 파일을 첨부할 때에는 똑같은 코드를 반복하여 각각 msg.attach() 해주면 됩니다.

      • [코드스니펫] - 파일 첨부하기

        part = MIMEBase('application', "octet-stream")
        with open("articles.xlsx", 'rb') as file:
               part.set_payload(file.read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment", filename="추석기사.xlsx")
        msg.attach(part)

       

 

 

 

 

  •  
    •  
    •  
    • 내 컴퓨터에서의 파일명과 실제로 보여지는 첨부 파일명은 달라도 된답니다!  

  •