您的位置:首页 > 编程语言 > Python开发

python每日练习7.4:爬取图片(成功运行)

2018-07-04 09:32 295 查看
代码原地址:https://blog.csdn.net/qq_34100655/article/details/78822272,感谢作者分享。
import os
import requests
from bs4 import BeautifulSoup

folder = '每日一练'#创建文件夹
if not os.path.exists(folder):
os.makedirs(folder)
def download(url, n):#设置将爬取到的图片保存到文件夹中
response = requests.get(url)
#name = url.split('/')[-1]
f = open(folder + '/' + str(n) + '.jpg', 'wb')
f.write(response.content)
f.close()
return True

n = 1
for i in range(1,3):
url_tieba = 'https://tieba.baidu.com/p/5431979599?pn=' + str(i)
header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
#伪装成浏览器

response_tieba = requests.get(url_tieba)
html_tieba = response_tieba.text
soup_tieba = BeautifulSoup(html_tieba, 'html.parser')
img_list = soup_tieba.find_all('img', attrs = {'class':'BDE_Image'})

for img in img_list:
print(n)
src = img.get('src')#<img src="/i/eg_tulip.jpg" />,src为标签属性
print(src)
download(src, n)
n += 1

print('OK')
网页代码分析:
<img class="BDE_Image" pic_type="0" width="484" height="296" src="https://imgsa.baidu.com/forum/w%3D580/sign=3c8742a9d654564ee565e43183df9cde/fddc8f19ebc4b745367ac9acc4fc1e178b821523.jpg">


阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐