내일배움캠프 10일차 개발일지

2021. 9. 29. 23:35스파르타코딩클럽

# 현상황

 

팀 프로젝트를 진행함으로서 시간이 항상 부족해진것 같다. 그래도 그날그날 손댔던 코드들을 올리는것이 좋겠다.

 

# TIL

 

사진을 크롤링하여 지정 폴더에 저장
참고
https://rekt77.tistory.com/category/Dev/python%20%EC%9B%B9%20%ED%81%AC%EB%A1%A4%EB%A7%81
저장경로를 설정하는법을 몰라 헤맸었다... 자료를 잘 찾아보는 법을 길러야지...

def getPic():
    users = list(db.userInfo.find({}, {'_id': False}))
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}

    for one in users:
        name = one['name']
        url = one['url']
        data = requests.get(url, headers=headers)
        soup = BeautifulSoup(data.text, 'html.parser')
        image = soup.select_one('meta[property="og:image"]')['content']

        imgUrl = image

        # urlretrieve는 다운로드 함수
        urllib.request.urlretrieve(imgUrl, "static/images/" + name + '.jpg')

        db.userInfo.update_one({'name': name}, {'$set': {'pic': '../static/images/' + name + '.jpg'}})​