1.Requests库入门
Requests安装
用管理员身份打开命令提示符:
pip install requests
测试:打开IDLE:
>>> import requests
>>> r = requests.get("http://www.baidu.com")
>>> r.status_code
200
>>> r.encoding = 'utf-8' #修改默认编码
>>> r.text#打印网页内容
HTTP协议
超文本传输协议,Hypertext Transfer Protocol.
HTTP是一个基于“请求与响应”模式的、无状态的应用层协议。
HTTP协议采用URL作为定位网络资源的标识。
URL格式
http://host[:port][path]
host:合法的Internet主机域名或IP地址
port:端口号,缺省端口为80
path:请求资源的路径
操作
方法 | 说明 |
---|---|
GET | 请求获取URL位置的资源 |
HEAD | 请求获取URl位置资源的响应消息报告,即获得该资源的头部信息 |
POST | 请求向URL位置的资源后附加新的数据 |
PUT | 请求向URL位置存储一个资源,覆盖原URL位置的资源 |
PATCH | 请求局部更新URL位置的资源,即改变该处资源的部分内容 |
DELETE | 请求删除URL位置存储的资源 |
Requests主要方法
方法 | 说明 |
---|---|
requests.request() | 构造一个请求,支撑以下各方法的基础方法 |
requests.get() | 获取HTML网页的主要方法,对应于HTTP的GET |
requests.head() | 获取HTML网页头信息的方法,对应于HTTP的HEAD |
requests.post() | 向HTML网页提交POST请求的方法,对应于HTTP的POST |
requests.put() | 向HTML网页提交PUT请求的方法,对应于HTTP的PUT |
requests.patch() | 向HTML网页提交局部修改请求,对应于HTTP的PATCH |
requests.delete() | 向HTML网页提交删除请求,对应于HTTP的DELETE |
主要方法为request方法,其他方法都是在此方法基础上封装而来以便使用。
request()方法
requests.request(method,url,**kwargs)
#method:请求方式,对应get/put/post等7种
#url:拟获取页面的url链接
#**kwargs:控制访问的参数,共13个
**kwargs:控制访问的参数,均为可选项
get()方法
r = requests.get(url)
完整方法:
requests.get(url,params=None,**kwargs)
url:拟获取页面的url链接
params:url中的额外参数,字典或字节流格式,可选
**kwargs:12个控制访问的参数,可选
get()方法:
构造一个向服务器请求资源的Request对象
返回一个包含服务器资源的Response对象
Response对象
属性 | 说明 |
---|---|
r.status_code | HTTP请求的返回状态,200表示连接成功,404表示失败 |
r.text | HTTP响应内容的字符串形式,即:url对应的页面内容 |
r.encoding | 从HTTP header中猜测的响应内容编码方式 |
r.apparent_encoding | 从内容中分析出的响应内容编码方式(备选编码方式) |
r.content | HTTP响应内容的二进制形式 |
head()方法
r = requests.head('http://httpbin.org/get')
r.headers
获取网络资源的概要信息
post()方法
向服务器提交新增数据
payload = {'key1':'value1','key2':'value2'} #新建一个字典
#向URL POST一个字典,自动编码为form(表单)
r = requests.post('http://httpbin.org/post',data = payload)
#向URL POST一个字符串,自动编码为data
r = requests.post('http://httpbin.org/post',data = 'ABC')
print(r.text)
put()方法
同post,只不过会把原来的内容覆盖掉。
patch()方法
delete()方法
Requests库的异常
异常 | 说明 |
---|---|
requests.ConnectionError | 网络连接错误异常,如DNS查询失败、拒绝连接等 |
requests.HTTPError | HTTP错误异常 |
requests.URLRequired | URL缺失异常 |
requests.TooManyRedirects | 超过最大 重定向次数,产生重定向异常 |
requests.ConnectTimeout | 连接远程服务器超时异常 |
requests.Timeout | 请求URL超时,产生超时异常 |
异常方法 | 说明 |
---|---|
r.raise_for_status | 如果不是200产生异常requests.HTTPError |
爬取网页的通用代码框架
import requestsdef getHTMLText(url):
try:
r = requests.get(url,timeout=30)
r.raise_for_status() #如果不是200,引发HTTPError异常
r.recoding = r.apparent_encoding
return r.text
except:
return "产生异常"
if __name__ == "__main__":
url = "http://www.baidu.com"
print(getHTMLText(url))
实例
向百度提交关键词
import requests# 向搜索引擎进行关键词提交
url = "http://www.baidu.com"
try:
kv = {'wd':'python'}
r = requests.get(url,params =kv)
print(r.request.url)
r.raise_for_status()
print(len(r.text))
except:
print("产生异常")
获取网络图片及存储
import requests
import os
url = "http://image.ngchina.com.cn/2019/0423/20190423024928618.jpg"
root = "D://2345//Temp//"
path = root + url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r = requests.get(url)
with open(path,'wb') as f:
f.write(r.content) #r.content返回二进制内容
f.close()
print("文件保存成功")
else:
print("文件已存在")
except:
print("爬取失败")
2.信息提取之Beautiful Soup库入门
Beautiful Soup库安装
pip install beautifulsoup4
测试:
import requests
r = requests.get("http://python123.io/ws/demo.html")
demo = r.text
form bs4 import BeautifulSoup #从bs4中引入BeautifulSoup类
soup = BeautifulSoup(demo, "html.parser")
Beautiful Soup库是解析、遍历、维护“标签树”的功能库
Beautiful Soup库的基本元素
Beautiful Soup库的引用
Beautiful Soup库,也叫beautifulsoup4或bs4.
from bs4 import BeautifulSoup
soup = BeautifulSoup(demo,"html.parser")
Beautiful Soup类的基本元素
基本元素 | 说明 |
---|---|
Tag | 标签,最基本的信息组织单元,分别用<>和</>标明开头和结尾 |
Name | 标签的名字,
… 的名字是’p’,格式:.name |
Attributes | 标签的属性,字典形式组织,格式:.attrs |
NavigableString | 标签内非属性字符串,<>…</>中字符串,格式:.string |
Comment | 标签内字符串的注释部分,一种特殊的Comment类型 |
基于bs4库的HTML内容遍历方法
下行遍历
属性 | 说明 |
---|---|
.contents(列表类型) | 子节点的列表,将所有儿子节点存入列表 |
.children | 子节点的迭代类型,与.contents类似,用于循环遍历儿子节点 |
.descendants | 子孙节点的迭代类型,包含所有子孙节点,用于循环遍历 |
#遍历儿子节点
for child in soup.body.children
print(child)
#遍历子孙节点
for child in soup.body.descendants
print(child)
上行遍历
属性 | 说明 |
---|---|
.parent | 节点的父亲标签 |
.parents | 节点先辈标签的迭代类型,用于循环遍历先辈节点 |
soup = BeautifulSoup(demo,"html.parser")
for parent in soup.a.parents:
if parent is None:
print(parent)
else:
print(parent.name)
#输出结果
#p
#body
#html
#[document]
平行遍历
平行遍历发生在同一个父节点下的各节点间。
下一个获取的可能是字符串类型,不一定是下一个节点。
属性 | 说明 |
---|---|
.next_sibling | 返回按照HTML文本顺序的下一个平行节点标签 |
.previous_sibling | 返回按照HTML文本顺序的上一个平行节点标签 |
.next_siblings | 迭代类型,返回按照HTML文本顺序的后续所有平行节点标签 |
.previous_siblings | 迭代类型,返回按照HTML文本顺序的前续所有平行节点标签 |
#遍历后续节点
for sibling in soup.a.next_siblings
print(sibling)
#遍历前续节点
for sibling in soup.a.previous_siblings
print(sibling)
基于bs4库的HTML格式化和编码
格式化方法:.prettify()
soup = BeautifulSoup(demo,"html.parser")
print(soup.a.prettify())
编码:默认utf-8
soup = BeautifulSoup("<p>中文</p>","html.parser")
soup.p.string
#'中文'
print(soup.p.prettify())
#<p>
# 中文
#</p>
3.信息组织与提取
信息标记的三种形式
标记后的信息可形成信息组织结构,增加了信息的维度;
标记后的信息可用于通信、存储和展示;
标记的结构和信息一样具有重要价值;
标记后的信息有利于程序的理解和运用。
XML: eXtensible Matkup Language
最早的通用信息标记语言,可扩展性好,但繁琐。
用于Internet上的信息交互和传递。
<name>...</name>
<name/>
<!-- -->
JSON: JavaScript Object Notation
信息有类型,适合程序处理(js),较XML简洁。
用于移动应用云端和节点的信息通信,无注释。
#有类型的键值对表示信息的标记形式
"key":"value"
"key":["value1","value2"]
"key":{"subkey":"subvalue"}
YAMl: YAML Ain’t Markup Language
信息无类型,文本信息比例最高,可读性好。
用于各类系统的配置文件,有注释易读。
#无类型的键值对表示信息的标记形式
key : "value"
key : #comment
-value1
-value2
key :
subkey : subvalue
信息提取的一般方法
方法一:完整解析信息的标记形式,再提取关键信息。
XML JSON YAML
需要标记解析器,例如bs4库的标签树遍历。
优点:信息解析准确
缺点:提取过程繁琐,过程慢
方法二:无视标记形式,直接搜索关键信息
搜索
对信息的文本查找函数即可。
优点:提取过程简洁,速度较快
缺点:提取过程准确性与信息内容相关
融合方法:结合形式解析与搜索方法,提取关键信息
XML JSON YAML 搜索
需要标记解析器及文本查找函数。
实例:提取HTML中所有URL链接
思路:1. 搜索到所有https://finance.sina.com.cn/stock/
百度股票:https://gupiao.baidu.com/stock/ 选取原则:股票信息静态存在于HTML页面中,非js代码生成,没有Robots协议限制。
程序的结构设计
- 步骤1:从东方财富网获取股票列表
- 步骤2:根据股票列表逐个到百度股票获取个股信息
- 步骤3:将结果存储到文件
初步代码编写(error)
import requests
from bs4 import BeautifulSoup
import traceback
import redef getHTMLText(url):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""def getStockList(lst, stockURL):
html = getHTMLText(stockURL)
soup = BeautifulSoup(html, 'html.parser')
a = soup.find_all('a')
for i in a:
try:
href = i.attrs['href']
lst.append(re.findall(r"[s][hz]\d{6}", href)[0])
except:
continuedef getStockInfo(lst, stockURL, fpath):
for stock in lst:
url = stockURL + stock + ".html"
html = getHTMLText(url)
try:
if html=="":
continue
infoDict = {}
soup = BeautifulSoup(html, 'html.parser')
stockInfo = soup.find('div',attrs={'class':'stock-bets'}) name = stockInfo.find_all(attrs={'class':'bets-name'})[0]
infoDict.update({'股票名称': name.text.split()[0]}) keyList = stockInfo.find_all('dt')
valueList = stockInfo.find_all('dd')
for i in range(len(keyList)):
key = keyList[i].text
val = valueList[i].text
infoDict[key] = val with open(fpath, 'a', encoding='utf-8') as f:
f.write( str(infoDict) + '\n' )
except:
traceback.print_exc()
continuedef main():
stock_list_url = 'https://quote.eastmoney.com/stocklist.html'
stock_info_url = 'https://gupiao.baidu.com/stock/'
output_file = 'D:/BaiduStockInfo.txt'
slist=[]
getStockList(slist, stock_list_url)
getStockInfo(slist, stock_info_url, output_file)main()
代码优化(error)
速度提高:编码识别的优化
import requests
from bs4 import BeautifulSoup
import traceback
import redef getHTMLText(url, code="utf-8"):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = code
return r.text
except:
return ""def getStockList(lst, stockURL):
html = getHTMLText(stockURL, "GB2312")
soup = BeautifulSoup(html, 'html.parser')
a = soup.find_all('a')
for i in a:
try:
href = i.attrs['href']
lst.append(re.findall(r"[s][hz]\d{6}", href)[0])
except:
continuedef getStockInfo(lst, stockURL, fpath):
count = 0
for stock in lst:
url = stockURL + stock + ".html"
html = getHTMLText(url)
try:
if html=="":
continue
infoDict = {}
soup = BeautifulSoup(html, 'html.parser')
stockInfo = soup.find('div',attrs={'class':'stock-bets'}) name = stockInfo.find_all(attrs={'class':'bets-name'})[0]
infoDict.update({'股票名称': name.text.split()[0]}) keyList = stockInfo.find_all('dt')
valueList = stockInfo.find_all('dd')
for i in range(len(keyList)):
key = keyList[i].text
val = valueList[i].text
infoDict[key] = val with open(fpath, 'a', encoding='utf-8') as f:
f.write( str(infoDict) + '\n' )
count = count + 1
print("\r当前进度: {:.2f}%".format(count*100/len(lst)),end="")
except:
count = count + 1
print("\r当前进度: {:.2f}%".format(count*100/len(lst)),end="")
continuedef main():
stock_list_url = 'https://quote.eastmoney.com/stocklist.html'
stock_info_url = 'https://gupiao.baidu.com/stock/'
output_file = 'D:/BaiduStockInfo.txt'
slist=[]
getStockList(slist, stock_list_url)
getStockInfo(slist, stock_info_url, output_file)main()
测试成功代码
由于东方财富网链接访问时出现错误,所以更换了一个新的网站去获取股票列表,具体代码如下:
import requests
import re
import traceback
from bs4 import BeautifulSoup
import bs4def getHTMLText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return""def getStockList(lst, stockListURL):
html = getHTMLText(stockListURL)
soup = BeautifulSoup(html, 'html.parser')
a = soup.find_all('a')
lst = []
for i in a:
try:
href = i.attrs['href']
lst.append(re.findall(r"[S][HZ]\d{6}", href)[0])
except:
continue
lst = [item.lower() for item in lst] # 将爬取信息转换小写
return lstdef getStockInfo(lst, stockInfoURL, fpath):
count = 0
for stock in lst:
url = stockInfoURL + stock + ".html"
html = getHTMLText(url)
try:
if html == "":
continue
infoDict = {}
soup = BeautifulSoup(html, 'html.parser')
stockInfo = soup.find('div', attrs={'class': 'stock-bets'}) if isinstance(stockInfo, bs4.element.Tag): # 判断类型
name = stockInfo.find_all(attrs={'class': 'bets-name'})[0]
infoDict.update({'股票名称': name.text.split('\n')[1].replace(' ','')})
keylist = stockInfo.find_all('dt')
valuelist = stockInfo.find_all('dd')
for i in range(len(keylist)):
key = keylist[i].text
val = valuelist[i].text
infoDict[key] = val with open(fpath, 'a', encoding='utf-8') as f:
f.write(str(infoDict) + '\n')
count = count + 1
print("\r当前速度:{:.2f}%".format(count*100/len(lst)), end="")
except:
count = count + 1
print("\r当前速度:{:.2f}%".format(count*100/len(lst)), end="")
traceback.print_exc()
continuedef main():
fpath = 'D://gupiao.txt'
stock_list_url = 'https://hq.gucheng.com/gpdmylb.html'
stock_info_url = 'https://gupiao.baidu.com/stock/'
slist = []
list = getStockList(slist, stock_list_url)
getStockInfo(list, stock_info_url, fpath)main()
6.爬虫框架-Scrapy
爬虫框架:是实现爬虫功能的一个软件结构和功能组件集合。
爬虫框架是一个半成品,能够帮助用户实现专业网络爬虫。
安装Scrapy
pip install scrapy
#验证
scrapy -h
遇到错误
building 'twisted.test.raiser' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/
解决方案
-
查看python版本及位数
C:\Users\ASUS>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.可知,python版本为3.7.2, 64位
-
下载Twisted
到 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 下载twisted对应版本的whl文件;
根据版本应下载Twisted‑17.9.0‑cp37‑cp37m‑win_amd64.whl
注意:cp后面是python版本,amd64代表64位,32位的下载32位对应的文件。
-
安装Twisted
python -m pip install D:\download\Twisted‑19.2.0‑cp37‑cp37m‑win_amd64.whl
-
安装Scrapy
python -m pip install scrapy
Scrapy爬虫框架解析
- Engine:不需要用户修改
- 控制所有模块之间的数据流
- 根据条件触发事件
- Downloader:不需要用户修改
- 根据请求下载网页
- Scheduler:不需要用户修改
- 对所有爬取请求进行调度管理
- Downloader Middleware:用户可编写配置代码
- 目的:实施Engine、Scheduler和Downloader之间进行用户可配置的控制
- 功能:修改、丢弃、新增请求或响应
- Spider:需要用户编写配置代码
- 解析Downloader返回的响应(Response)
- 产生爬取项(scraped item)
- 产生额外的爬取请求(Request)
- Item Pipelines:需要用户编写配置代码
- 以流水线方式处理Spider产生的爬取项
- 由一组操作顺序组成,类似流水线,每个操作是一个Item Pipeline类型
- 可能操作包括:清理、检验、和查重爬取项中的HTML数据、将数据存储到数据库
- Spider Middleware:用户可以编写配置代码
- 目的:对请求和爬取项的再处理
- 功能:修改、丢弃、新增请求或爬取项
requests vs. Scrapy
-
相同点
- 两者都可以进行页面请求和爬取,Python爬虫的两个重要技术路线
- 两者可用性都好,文档丰富,入门简单
- 两者都没有处理js、提交表单、应对验证码等功能(可扩展)
-
不同点
requests Scrapy 页面级爬虫 网站级爬虫 功能库 框架 并发性考虑不足,性能较差 并发性好,性能较高 重点在于页面下载 重点在于爬虫结构 定制灵活 一般定制灵活,深度定制困难 上手十分简单 入门稍难 Scrapy爬虫的常用命令
Scrapy命令行
Scrapy是为持续运行设计的专业爬虫框架,提供操作的Scrapy命令行
命令 | 说明 | 格式 |
---|---|---|
startproject | 创建一个新工程 | scrapy startproject [dir] |
genspider | 创建一个爬虫 | scrapy genspider [options] |
settings | 获得爬虫配置信息 | scrapy setting [options] |
crawl | 运行一个爬虫 | scrapy crawl |
list | 列出工程中所有爬虫 | scrapy list |
shell | 启动URL调试命令行 | scrapy shell [url] |
Scrapy框架的基本使用
步骤1:建立一个Scrapy爬虫工程
#打开命令提示符-win+r 输入cmd
#进入存放工程的目录
D:\>cd demo
D:\demo>
#建立一个工程,工程名python123demo
D:\demo>scrapy startproject python123demo
New Scrapy project 'python123demo', using template directory 'd:\program files\python\lib\site-packages\scrapy\templates\project', created in:
D:\demo\python123demoYou can start your first spider with:
cd python123demo
scrapy genspider example example.com
D:\demo>
生成的目录工程介绍:
python123demo/ —————-> 外层目录
scrapy.cfg ———> 部署Scrapy爬虫的配置文件
python123demo/ ———> Scrapy框架的用户自定义Python代码
__init__.py
—-> 初始化脚本items.py —-> Items代码模板(继承类)
middlewares.py —-> Middlewares代码模板(继承类)
pipelines.py —-> Pipelines代码模板(继承类)
settings.py —-> Scrapy爬虫的配置文件
spiders/ —-> Spiders代码模板目录(继承类)
spiders/—————-> Spiders代码模板目录(继承类)
__init__.py
——–> 初始文件,无需修改
__pycache__/
——–> 缓存目录,无需修改
步骤2:在工程中产生一个Scrapy爬虫
#切换到工程目录
D:\demo>cd python123demo
#产生一个scrapy爬虫
D:\demo\python123demo>scrapy genspider demo python123.io
Created spider 'demo' using template 'basic' in module:
python123demo.spiders.demoD:\demo\python123demo>
步骤3:配置产生的spider爬虫
修改D:\demo\python123demo\python123demo\spiders\demo.py
# -*- coding: utf-8 -*-
import scrapyclass DemoSpider(scrapy.Spider):
name = 'demo'
# allowed_domains = ['python123.io']
start_urls = ['http://python123.io/ws/demo.html'] def parse(self, response):
fname = response.url.split('/')[-1]
with open(fname, 'wb') as f:
f.write(response.body)
self.log('Save file %s.' % name)
完整版代码编写方式
import scrapyclass DemoSpider(scrapy.Spider):
name = "demo" def start_requests(self):
urls = [
'http://python123.io/ws/demo.html'
]
for url in urls:
yield scrapy.Requests(url=url, callback=self.parse) def parse(self, response):
fname = response.url.split('/')[-1]
with open(fname, 'wb') as f:
f.write(response.body)
self.log('Saved file %s.' % fname)
步骤4:运行爬虫,获取网页
#输入运行命令 scrapy crawl
D:\demo\python123demo>scrapy crawl demo
可能出现的错误
ModuleNotFoundError: No module named 'win32api'
解决方法
-
到 https://pypi.org/project/pypiwin32/ 下载py3版本的pypiwin32-223-py3-none-any.whl文件;
-
安装pypiwin32-223-py3-none-any.whl
pip install D:\download\pypiwin32-223-py3-none-any.whl
-
再次在工程目录下运行爬虫
scrapy crawl demo
yield关键字的使用
- yield<———————–>生成器
- 生成器是一个不断产生值的函数;
- 包含yield语句的函数是一个生成器;
- 生成器每次产生一个值(yield语句),函数会被冻结,被唤醒后再产生一个值;
实例:
def gen(n):
for i in range(n):
yield i**2
#产生小于n的所有2的平方值
for i in gen(5):
print(i, " ", end="")
#0 1 4 9 16
#普通写法
def square(n):
ls = [i**2 for i in range(n)]
return ls
for i in square(5):
print(i, " ", end="")
#0 1 4 9 16
为何要有生成器?
- 生成器比一次列出所有内容的优势
- 更节省存储空间
- 响应更迅速
- 使用更灵活
Scrapy爬虫的使用步骤
- 步骤1:创建一个工程和Spider模板;
- 步骤2:编写Spider;
- 步骤3:编写Item Pipeline
- 步骤4:优化配置策略
Scrapy爬虫的数据类型
Request类
class scrapy.http.Request()
- Request对象表示一个HTTP请求
- 由Spider生成,由Downloader执行
属性或方法 | 说明 |
---|---|
.url | Request对应的请求URL地址 |
.method | 对应的请求方法,’GET‘ ’POST‘等 |
.headers | 字典类型风格的请求头 |
.body | 请求内容主体,字符串类型 |
.meta | 用户添加的扩展信息,在Scrapy内部模块间传递信息使用 |
.copy() | 复制该请求 |
Response类
class scrapy.http.Response()
- Response对象表示一个HTTP响应
- 由Downloader生成,由Spider处理
属性或方法 | 说明 |
---|---|
.url | Response对应的URL地址 |
.status | HTTP状态码,默认是200 |
.headers | Response对应的头部信息 |
.body | Response对应的内容信息,字符串类型 |
.flags | 一组标记 |
.request | 产生Response类型对应的Request对象 |
.copy() | 复制该响应 |
Item类
class scrapy.item.Item()
- Item对象表示一个从HTML页面中提取的信息内容
- 由Spider生成,由Item Pipeline处理
- Item类似字典类型,可以按照字典类型操作
CSS Selector的基本使用
.css(‘a::attr(href)’).extract()
CSS Selector由W3C组织维护并规范。
股票数据Scrapy爬虫实例
功能描述:
- 技术路线:scrapy
- 目标:获取上交所和深交所所有股票的名称和交易信息
- 输出:保存到文件中
实例编写
- 步骤1:首先进入命令提示符建立工程和Spider模板
scrapy startproject BaiduStocks
cd BaiduStocks
scrapy genspider stocks baidu.com
#进一步修改spiders/stocks.py文件
- 步骤2:编写Spider
- 配置stock.py文件
- 修改对返回页面的处理
- 修改对新增URL爬取请求的处理
打开spider.stocks.py文件
# -*- coding: utf-8 -*-
import scrapy
import reclass StocksSpider(scrapy.Spider):
name = "stocks"
start_urls = ['https://quote.eastmoney.com/stocklist.html'] def parse(self, response):
for href in response.css('a::attr(href)').extract():
try:
stock = re.findall(r"[s][hz]\d{6}", href)[0]
url = 'https://gupiao.baidu.com/stock/' + stock + '.html'
yield scrapy.Request(url, callback=self.parse_stock)
except:
continue def parse_stock(self, response):
infoDict = {}
stockInfo = response.css('.stock-bets')
name = stockInfo.css('.bets-name').extract()[0]
keyList = stockInfo.css('dt').extract()
valueList = stockInfo.css('dd').extract()
for i in range(len(keyList)):
key = re.findall(r'>.*</dt>', keyList[i])[0][1:-5]
try:
val = re.findall(r'\d+\.?.*</dd>', valueList[i])[0][0:-5]
except:
val = '--'
infoDict[key]=val infoDict.update(
{'股票名称': re.findall('\s.*\(',name)[0].split()[0] + \
re.findall('\>.*\<', name)[0][1:-1]})
yield infoDict
- 步骤3:编写Pipelines
- 配置pipelines.py文件
- 定义对爬取项(Scrapy Item)的处理类
- 配置ITEM_PIPELINES选项
pipelines.py
# -*- coding: utf-8 -*-# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.htmlclass BaidustocksPipeline(object):
def process_item(self, item, spider):
return itemclass BaidustocksInfoPipeline(object):
def open_spider(self, spider):
self.f = open('BaiduStockInfo.txt', 'w') def close_spider(self, spider):
self.f.close() def process_item(self, item, spider):
try:
line = str(dict(item)) + '\n'
self.f.write(line)
except:
pass
return item
setting.py
# Configure item pipelines
# See https://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
'BaiduStocks.pipelines.BaidustocksInfoPipeline': 300,
}
配置并发连接选项
settings.py
选项 | 说明 |
---|---|
CONCURRENT_REQUESTS | Downloader最大并发请求下载数量,默认为32 |
CONCURRENT_ITEMS | Item Pipeline最大并发ITEM处理数量,默认为100 |
CONCURRENT_REQUESTS_PRE_DOMAIN | 每个目标域名最大的并发请求数量,默认为8 |
CONCURRENT_REQUESTS_PRE_IP | 每个目标IP最大的并发请求数量,默认为0,非0有效 |
来源:中国大学MOOC-北京理工大学-嵩天-Python网络爬虫与信息提取