欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python学习--第八天

程序员文章站 2022-07-09 23:36:51
...

1.爬取豆瓣电影排行榜单https://movie.douban.com/chart的各电影海报到本地image文件夹下。

import os
import bs4
import requests
url = 'https://movie.douban.com/chart'
head = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                      'AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/80.0.3987.132 Safari/537.36'
}
reqe = requests.get(url=url, headers=head)
reqe.encoding = 'utf-8'
soup = bs4.BeautifulSoup(reqe.text, 'html.parser')
div_items = []
folder = 'image'
g = 1
try:
    os.mkdir(folder)
except:
    folder = 'image(' + str(g) + ')'
    g += 1
    os.mkdir(folder)
    os.chdir(folder)
for each in soup.find_all(name='img', attrs={'class': 'm_sub_img'}):
    div_items.append(each['src'])
for i in range(0, 100):#需要爬取的图片数
    with open("./image"+str(i)+'.jpg', 'wb') as f:
        img_html = requests.get(div_items[i])
        f.write(img_html.content)
        i += 1
相关标签: Python