爬虫1:html页面+beautifulsoap模块+get方式+demo
前言:最近公司要求编写一个爬虫,需要完善后续金融项目的数据,由于工作隐私,就不付被爬的网址url了,下面总结下spider的工作原理。
语言:python;工具:jupyter;
概要:说到爬虫spider,就不得不提html页面的解析,说到html页面的解析就不得不提beautifulsoap模块的使用,其对html页面的解析很到位,可以很方便的定位需要爬取的元素。
BeautifulSoap的API: https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
demo流程:
(1)使用requests模块,获取url页面。
import requests
url = "http://www.~~~~~~~~~~~~~~~~~~~~~~~~~~"
r = requests.get(url)
(2)解析html页面(若是pdf页面需要其他工具模块)需要使用BeautifulSoup模块,把request下来的页面信息保存为soup格式。
from bs4 import BeautifulSoup
soup = BeautifulSoup(r.text)
(3)利用soup找到超链接href 并把href保存到文件中,为了后续的使用;
with open(r"E:\aa.txt", "wb") as code:
for link in soup.find_all('a'):
code.write(str(link.get('href')) + '\r\n')
print "Download Complete!"
(4)在上一步的文件中,读取保存的href连接,并保存到list数据结构中;
fd = open(r"E:\juchao.txt","r")
mylist = []for line in fd:
mylist.append(line)
(5)编写header,为了post方式伪装成浏览器(必要的话,设置参数data);并拼接成访问的url格式(利用浏览器调试,查看网络中的信息);
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Connection': 'keep-alive',
'Cookie': 'JSESSIONID=27AF575249A833C368677F9B5869A463',
'Host': 'www.cninfo.com.cn',
'Referer': 'http://www.~~~~~~~~~~~~~~~',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Content-Length': '',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
}
urlpath = 'http://www.cninfo.com.cn/information/brief/szmb'
myUrls = []
for submylist in mylist:
urlId = ''
url = ''
urlId = submylist[-7:-1]
url = urlpath + urlId + '.html'
myUrls.append(url)
(6)新拼接的url是我们需要的最终页面,requests获取url页面(注意编码问题),利用soup解析html页面,生成json字符串,保存到文件。
import json
with open(r"E:\juchao_json.txt", "wb") as code: for k in xrange(len(myUrls)):
r1 = requests.get(myUrls[k])
r1.encoding = r1.apparent_encoding
# print r1.encoding soup = BeautifulSoup(r1.text)
jsonMap = {}
jsonMapKey = []
jsonMapValue = []
for i in soup.select(".zx_data"):
jsonMapKey.append(i.text) for i in soup.select(".zx_data2"):
jsonMapValue.append(i.text[:-]) for j in xrange(len(jsonMapKey)):
jsonMap[jsonMapKey[j]] = jsonMapValue[j] strJson = json.dumps(jsonMap, ensure_ascii=False)
# print strJson
code.write(strJson.encode('utf-8') + '\r\n') print 'Done!'
BeautifulSoap常用API:推荐查看官方文档,见上
1)安装: pip install BeautifulSoap
2 )对象:Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象.
所有对象可以归纳为4种: Tag , NavigableString ,BeautifulSoup , Comment
3)遍历文档:.tag .contents .children .descendants .parent .parents .next_slibling .previous_slibling .next_element
4)搜索文档:find() find_all() find_parents find_next_siblings select
descendants
最新文章
- webstorm 配置scss的问题
- 有关于psExec的使用
- Apache Kafka - Quick Start on Windows
- flask学习
- Android -- 经验分享(二)
- 清除IE中Ajax缓存,Chrome不需要
- Linux日常使用指令大全
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.解决办法
- cygwin中vim的使用方法
- Appcan开发笔记:结合JQuery的$.Deferred()完善批量异步发送
- 高质量PHP代码的50个实用技巧必备(下)
- [翻译] 编写高性能 .NET 代码--第二章 GC -- 减少分配率, 最重要的规则,缩短对象的生命周期,减少对象层次的深度,减少对象之间的引用,避免钉住对象(Pinning)
- hdu 3939(勾股+容斥)
- Leetcode#191. Number of 1 Bits(位1的个数)
- DP E - Cheapest Palindrome
- LeetCode--008--字符串转换整数 (atoi)(java)
- C++基础知识:构造与析构
- Android开发 ---xml布局元素
- 详解C#泛型(三)
- C++11 自动推导auto