Python爬虫 - Beautiful Soup4(二)-网络文件爬取
2017-11-25 15:26
295 查看
from urllib import request
from bs4 imp
4000
ort BeautifulSoup
url = 'http://python.org/'
# 下载网页
print ("连接网络")
html = request.urlopen(url)
print ("开始下载网页")
content = html.read()
content = content.decode('utf-8')
print ("下载网页完成")
html.close()
# 使用BeautifulSoup匹配图片
html_soup = BeautifulSoup(content,'lxml')
# 相较通过正则表达式去匹配,BeautifulSoup提供了一个更简单灵活的方式
all_img_links = html_soup.findAll('img')
print (all_img_links)
# 接下来就是老生常谈的下载图片
img_counter = 1
for img_link in all_img_links:
img_name = '%s.jpg' % img_counter
# 下载到本地 默认下载在文件相对路径
request.urlretrieve(url+img_link['src'], img_name)
img_counter += 1
from bs4 imp
4000
ort BeautifulSoup
url = 'http://python.org/'
# 下载网页
print ("连接网络")
html = request.urlopen(url)
print ("开始下载网页")
content = html.read()
content = content.decode('utf-8')
print ("下载网页完成")
html.close()
# 使用BeautifulSoup匹配图片
html_soup = BeautifulSoup(content,'lxml')
# 相较通过正则表达式去匹配,BeautifulSoup提供了一个更简单灵活的方式
all_img_links = html_soup.findAll('img')
print (all_img_links)
# 接下来就是老生常谈的下载图片
img_counter = 1
for img_link in all_img_links:
img_name = '%s.jpg' % img_counter
# 下载到本地 默认下载在文件相对路径
request.urlretrieve(url+img_link['src'], img_name)
img_counter += 1
相关文章推荐
- 【Python网络爬虫开发教程】Beautiful Soup 4.2.0 文档
- python 网络爬虫 Beautiful Soup用法
- mac os平台使用python爬虫自动下载巨潮网络文件
- 【网络爬虫】【python】网络爬虫(四):scrapy爬虫框架(架构、win/linux安装、文件结构)
- Python爬虫入门实战系列(一)--爬取网络小说并存放至txt文件
- Python 网络爬虫 010 (高级功能) 解析 robots.txt 文件
- Python 网络爬虫 010 (高级功能) 解析 robots.txt 文件
- Python爬虫 - Beautiful Soup4(一)-本地文件爬取
- python网络爬虫之使用scrapy下载文件
- Python爬虫系列之----Scrapy(五)网页提取的三种方式(正则,Beautiful Soup,Lxml)
- Python网络爬虫——-2.网络爬虫基本原理
- Python网络爬虫框架scrapy的学习
- python-网络爬虫初学四:cookie的存储与读取
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
- python之网络爬虫
- Python 网络爬虫 006 (编程) 解决下载(或叫:爬取)到的网页乱码问题
- Python爬虫——2017高校网络信息安全管理运维挑战赛:快速计算
- [Python]网络爬虫(九):百度贴吧的网络爬虫(v0.4)源码及解析
- [python]简单的网络爬虫
- 第三百四十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制