[Python]网络数据采集概述(1)—页面访问及页面元素的解析
2017-08-30 20:46
423 查看
urlib
主要用途
BeautifulSoup
正则表达式
获取属性
Lambda表达式
其他的解析库
lxml
HTML parser
数据采集
单网站递归遍历内链
通过互联网采集外链
解析Json数据
参考书籍:
《Python网络数据采集》
主要用途
BeautifulSoup
正则表达式
获取属性
Lambda表达式
其他的解析库
lxml
HTML parser
数据采集
单网站递归遍历内链
通过互联网采集外链
解析Json数据
urlib
Python3.x整合了Python2中的urllib2和urllib,合并为urllib,并分为四大主要模块:urllib.request、urllib.error、urllib.parse和urllib.robotparser。
主要用途:
urllib.request:用于打开和读取URLs; urllib.error:包含urllib.request引发的异常; urllib.parse:用于解析URLs; urllib.robotparse:用于解析robot.txt文件。
BeautifulSoup
作用类似于正则表达式,通过定位HTML标签来格式化和组织复杂的网络信息,用Python对象来展现XML结构信息。 可以直接通过标签的属性来定位标签,获取标签内容。标签之间有层次关系。
from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen("http://www.pythonscraping.com/pages/page1.html") bsObj = BeautifulSoup(html.read()) print(bsObj.h1) nameList = bsObj.findAll("span",{"class":{"green", "red"}}) # findAll(tag, attributes, recursive, text, limit, 4000 keywords) # find(tag, attributes, recursive, text, keywords) for name in nameList: print(name.get_text()) for child in bsObj.find("table", {"id" : "giftList"}).children: # children:子代标签, descendants:后代标签 print(child) for sibling in bsObj.find("table", {"id" : "giftList"}).tr.next_siblings: # 兄弟标签 print(sibling) # 父标签 print(bsObj.find("img", {"src" : "../img/gifts/img1.jpg"}).parent.previous_sibling.get_text())
正则表达式
html = urlopen("http://www.pythonscraping.com/pages/page3.html") bsObj = BeautifulSoup(html) images = bsObj.findAll("img", {"src": re.compile("\.\.\/img\/gifts/img.*\.jpg")}) # 正则表达式可以作为BeautifulSoup 语句的任意一个参数 for image in images: print(image["src"])
获取属性
html = urlopen("http://www.pythonscraping.com/pages/page3.html") bsObj = BeautifulSoup(html) myImgTags = bsObj.findAll("img", {"src": re.compile("\.\.\/img\/gifts/img.*\.jpg")}) for myImgTag in myImgTags: print(myImgTag) print(myImgTag.attrs["src"]) # .attrs返回的是一个Python 字典对象
Lambda表达式
BeautifulSoup 允许我们把特定函数类型当作findAll 函数的参数。唯一的限制条件是这些 函数必须把一个标签作为参数且返回结果是布尔类型。BeautifulSoup 用这个函数来评估它 遇到的每个标签对象,最后把评估结果为“真”的标签保留,把其他标签剔除。如:soup.findAll(lambda tag: len(tag.attrs) == 2)
其他的解析库
lxml
大部分源码由C语言编写,可用于解析HTML和XML文档。解析速度快
HTML parser
python自带的解析库
数据采集
单网站递归遍历(内链)
def getLinks(pageUrl): # 自动查找内链,递归遍历链接 pages = set() def getLinks(pageUrl): global pages html = urlopen( pageUrl) bsObj = BeautifulSoup(html) for link in bsObj.findAll("a", href=re.compile("^(https://baike.baidu.com/)")): if 'href' in link.attrs: if link.attrs['href'] not in pages: #我们遇到了新页面 newPage = link.attrs['href'] print(newPage) pages.add(newPage) getLinks(newPage) getLinks("")
通过互联网采集(外链)
解析Json数据
from urllib.request import urlopen import json def getCountry(ipAddress): response = urlopen("http://freegeoip.net/json/"+ipAddress).read().decode('utf-8') responseJson = json.loads(response) return responseJson.get("country_code") print(getCountry("50.78.253.58"))
参考书籍:
《Python网络数据采集》
相关文章推荐
- Python网络数据采集(2)——复杂HTML解析(1)
- Python网络数据采集2:复杂HTML解析
- [Python]网络数据采集概述(3)—穿越网页表单、登录窗口进行采集
- Python网络数据采集(2)——复杂HTML解析(2)
- [Python]网络数据采集概述(2)—存储数据及读取文档
- 国内6大网络信息采集和页面数据抓取工具
- Android 之 网络访问服务器,解析JSON数据(返回无乱码)
- 访问网络数据+json解析代码片段
- 网络访问之json数据解析
- 『python学习』python 网络数据采集笔记
- Python网络数据采集4(译者:哈雷)
- Python网络数据采集学习笔记1
- Python网络数据采集5(译者:哈雷)
- Python网络数据采集6(译者:哈雷)
- Python网络数据采集1(译者:哈雷)
- python 网络数据采集(6-9章)
- Python使用scrapy采集数据过程中放回下载过大页面的方法
- 有哪些好用的互联网数据抓取,数据采集,页面解析工具?
- Python网络数据采集
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示