Python-简单的爬虫案例(百度贴吧-图片)

#!/usr/bin/python
# coding:utf-8
# 实现一个简单的爬虫,爬取百度贴吧图片
import requests
import re
import urllib.request

def main():
    # 获取页面的URL
    url = 'http://tieba.baidu.com/p/2256306796'
    # 获取页面的css样式
    html = requests.get(url).text
    # 使用正则,将图片提取出来
    jpgReg = re.compile(r'<img.+?src="(.+?\.jpg)" width')
    jpgs = re.findall(jpgReg, html)
    # 循环将图片存在文件夹中
    count = 0
    for url in jpgs:
        urllib.request.urlretrieve(url, './package/%s.jpg' % count)
        count += 1

if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/longfei_2010/article/details/79727290