#!/usr/bin/env python
# coding: utf-8
## 爬虫实战1——爬取百度logo
import os
import requests
import bs4
url = 'https://www.baidu.com/'
r = requests.get(url)
r.encoding = 'utf-8'
r.text
soup = bs4.BeautifulSoup(r.text)
print(soup.prettify())
img = soup.select('#lg img')
if img == []:
print('找不到图片!')
else:
imgUrl ='https:' + img[0].get('src')
print('开始下载···')
res = requests.get(imgUrl)
os.makedirs('baidu', exist_ok=True)
imageFile = open(os.path.join('baidu', os.path.basename(imgUrl)), 'wb')
for chunk in res.iter_content(100000):
imageFile .write(chunk)
imageFile.close()
print('下载完成!')