2018年三月月 发布的文章
python 抓取数据入库 mysql,使用 pymysql
python 之 抓取豆瓣电影TOP250
目标:
- 抓取豆瓣电影TOP250,名次,电影名称,海报图片
- 把图片保存在当前目录下的 douban_top_250 文件夹下
- 保存一份排名txt,和一份json格式的txt
- 打包成 win 下可运行的 exe 文件
用python抓取糗事百科之糗图
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 |
#!/usr/local/bin/python3 # -*- coding: utf-8 -*- import requests import re import time import os # 糗事百科 糗图抓取 # https://www.qiushibaike.com/pic/ # https://www.qiushibaike.com/pic/page/2/?s=5067959 # 打开网页,返回页面内容 def get_html(url): s = requests.get(url) return s.text # print(get_html("https://www.qiushibaike.com/pic/")) # 从页面中抓取所有的图片 def get_img_list(html): r = re.compile(r'<div class="thumb">.*?<img src=(.*?) alt=.*?>.*?</div>', re.S) img_list = re.findall(r, html) return img_list # print(get_img_list(get_html("https://www.qiushibaike.com/pic/"))) # 保存图片到本地 def save_img(img_list, page): # 首先创建目录 day = time.strftime("%Y-%m-%d", time.localtime()) path = './qiu/' + day + '/page' + str(page) is_path_exist = os.path.exists(path) if not is_path_exist: os.makedirs(path) img_count = 0 for img in img_list: f = open(path + '/' + str(img_count) + '.jpg', 'wb') # 处理图片链接 img = img.strip('"') img_url = "https:" + img f.write(requests.get(img_url).content) f.close() img_count += 1 # save_img(get_img_list(get_html("https://www.qiushibaike.com/pic/")),1) start_url = "https://www.qiushibaike.com/pic/" start_page = int(input("请输入起始页:")) end_page = int(input("请输入结束页:")) print("开始下载...") start_time = time.time() for page in range(start_page, end_page + 1): # 拼接url url = start_url + 'page/' + str(page) html = get_html(url) img_list = get_img_list(html) save_img(img_list, page) end_time = time.time() print("下载结束") print("一共用时:"+str(int(end_time - start_time))+"秒") |
1 2 3 4 5 |
请输入起始页:1 请输入结束页:35 开始下载... 下载结束 一共用时:195秒 |
从1至35页,每页20张图片,一共用时195秒
立个 flag ,2018 好好学 python
2018 年 3 月 4 日。
近期评论