Python 网页下载器和解析器
来源:互联网 发布:天猫和淘宝的盈利模式 编辑:程序博客网 时间:2024/06/05 19:07
某教程网 python 爬虫视频 http://www.imooc.com/learn/563有段代码在3.X中有变化。在 3.x 版本中 ,把 2.x 版本的模块 urllib2 合并为 urllib.request 。
视频中原代码:
更改为 3.x 的版本:
# -*- coding: utf-8 -*- # python 3.5# 网页下载器测试import urllib.requestimport http.cookiejarurl = "http://www.baidu.com"print("第一种方法:最简洁方法")response1 = urllib.request.urlopen(url) #直接请求print(response1.getcode()) #网页状态吗,200表示成功print(len(response1.read())) #网页html内容print("第二种方法:添加data、http header")request = urllib.request.Request(url)request.add_header("user-agent", "Mozilla/5.5") #添加http的headerresponse2 = urllib.request.urlopen(url) #发送请求获取结果print(response2.getcode())print(len(response2.read()))print("第三种方法:")cj = http.cookiejar.CookieJar() #创建cookie容器opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) #创建openerurllib.request.install_opener(opener) #给urllib 安装 openerresponse3 = urllib.request.urlopen(url) #使用带有 cookie 的 urllib 访问网页print(response3.getcode())print(len(response3.read()))
网页解析测试:
# -*- coding: utf-8 -*- # python 3.5# 网页解析测试import refrom bs4 import BeautifulSouphtml_doc = """<html><head><title>The Dormouse's story</title></head><body><p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;and they lived at the bottom of a well.</p><p class="story">...</p>"""soup = BeautifulSoup(html_doc,'html.parser')print('获取所有链接')links = soup.find_all('a')for link in links: print(link.name,link['href'],link.get_text())print('获取lacie链接')link_node = soup.find('a',href='http://example.com/lacie')print(link_node.name,link_node['href'],link_node.get_text())print('正则匹配')link_node = soup.find('a',href=re.compile(r"ill"))print(link_node.name,link_node['href'],link_node.get_text())print('获取P段落文字')p_node = soup.find('p',class_="title")print(p_node.name,p_node.get_text())输出结果:获取所有链接a http://example.com/elsie Elsiea http://example.com/lacie Laciea http://example.com/tillie Tillie获取lacie链接a http://example.com/lacie Lacie正则匹配a http://example.com/tillie Tillie获取P段落文字p The Dormouse's story
附上一张简单爬虫框架图:
阅读全文
0 0
- Python 网页下载器和解析器
- Python爬虫之网页下载器网页解析器
- Python 网页解析和URL下载
- python 3.5网页下载器
- Python爬虫----网页解析器和BeautifulSoup第三方模块
- Python抓取和解析网页
- Python的网页下载器:urllib2.urlopen
- Python轻量级爬虫教程-网页下载器
- python : BeautifulSoup 网页HTML 解析器
- Python 网页解析器BeautifulSoup示例使用
- Python数据采集之网页解析器
- Python爬虫基础-网页解析器-02
- Python轻量级爬虫教程-网页解析器
- 【beautifulsoup】python标准库解析器解析网页问题解决
- Python爬虫----网页下载器和urllib2模块及对应的实例
- 利用Python抓取和解析网页(下)
- 利用Python抓取和解析网页(1)
- 利用Python抓取和解析网页(2)
- 【雅思备考】Task2——总纲领
- PHP笔记
- 识别边界类/控制类/实体类
- Linux搭建svn控制
- [BZOJ4712]洪水-树链剖分-动态规划-线段树
- Python 网页下载器和解析器
- val("") 清除文本框后无法写入数据问题
- moho pro 12中文破解版V12.4下载 | 二维动画制作工具 | moho pro 12.4下载含moho pro 12 注册机
- 说说共享单车
- 白话 C++
- 6. ZigZag Conversion
- Linux用户态和内核态
- Dart语言语法特性简介(1)
- ES6 Map类型