2021의 게시물 표시

[Python] the first argument must be callable 에러 해결

 정말 간단한 거라 간단히 적어둡니다. schedule에 do를 사용할 때 함수의 변수를 괄호로 넣으면 에러가 발생합니다. 그래서 (1)이 아니라 (2)와 같이 사용해주면 에러가 사라집니다. (1) 에러 발생하는 경우 schedule . every ( 2 ). minutes . do ( print ( str ( time . localtime ))) (2) 콤마로 써주면 됩니다. schedule . every ( 2 ). minutes . do ( print , str ( time . localtime ))

[Python] __name__ = '__main__'의 활용 방법

이미지
  https://azanewta.tistory.com/24 위의 링크를 참고해서 간단하게 테스트를 해봤습니다... 1000개 이상 사이트를 스크래이핑 하는데, py소스 하나에 밀어넣으면 곤란할 듯하여, 각 사이트별로 모듈화를 생각하고 있는데, 사용 방법을 잘 몰라서,, if __name__ = '__main__': 으로 넣어놓으면, 메인인 경우에만 테스트 프레이즈 구동이 됩니다. sub1.py가 서브파일, FP2021_MLT_Main.py가 메인 파일입니다. Sub1.py 1 2 3 4 5 6 7 8 9 10 11 def  test(a, b):      return  a + b   def  main():      print ( 'Test function executed' )     a  =   2     b  =   3      print ( "Test Value = %d" %test(a,b))   if  __name__  = =   '__main__' :     main() cs FP2021_MLT_Main.py 1 2 3 4 import  Sub1   a  =  Sub1.test( 1 , 2 ) print (a) cs

[Python] 크롤링 HTTP Forbidden 403 문제 해결

이미지
  코드에는 문제가 없는데 HTTP 403 Forbidden이 발생하길래.. 검색해보니까 서버사이드에서 저 같은 불법 크롤러를 거르는 메쏘드가 있는 모양입니다. 아래의 링크를 참고해서 해결했습니다 https://howtoworld.tistory.com/52 urllib에서 Request를 import 해줘야합니다. 1 2 3 4 def  hellenicshipping():     url  =  Request( "https://www.hellenicshippingnews.com/category/shipping-news/dry-bulk-market" ,headers = { 'User-Agent' :  'Mozilla/5.0' })     html  =  urlopen(url)     savefile  =   './hsn.txt' Colored by Color Scripter cs

[Python] Telegram Bot + Web scraper (beautiful soup) 개발 중

Telegram auto uploader + basic crawler .. ksg와 shipping news를 크롤링 하는데, 이후에 이걸 표준 모듈화해서 업로드 하려고 합니다. token 과 chat_id 를 삭제하고 업로드했습니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 import  schedule import  time import  telegram   import  bs4 from  urllib.request  import  urlopen   token  =   "Your token here" bot  =  telegram.Bot(token  =  token) #print(bot) updates  =  bot.get_updates() chat_id  =   "Your Chat Id here"   title_tag  =  [] text_tag  =  [] date_tag  =  []   bot.sendMessage(chat_id, "Server Intialized..." )   def  job():     now  =  time.localtime()      print ( "current time = " , str (now))     url  =   "https://www.shippingnewsnet.com/news/articleList.html?sc_sub_section_code=S2N1&view_type=sm"     html  =  urlopen(url)       bs_obj  =  bs4.BeautifulSou