python爬虫(二)

201数据库MongoDB

import pymongo
client = pymongo.MongoClient('localhost',27017)
walden = client['walden']
sheet_tab = walden['sheet_tab']

//插入数据库
path = ' '
with open(path,'r') as f:
    lines = f.readlines()
    for index, line_enumerate(lines):
        data = {
            'index':index,
            'line':line,
            'words':len(line,split())
        }
    sheet_tab.insert_one(data)

//查找
for item in sheet_tab.find('words',0):
   print(item['line'])

202爬取大规模数据的工作流

第一步:观察页面特征

不同页面不规则的问题

58同城的分页问题

第二步:设计工作流程

1.找到频道的所有链接

2.设置数据库

3.用spider1爬取商品链接

 4.用spider2爬取商品详情信息

404页识别

<script src="http://static.58.com/is/404/topbar_404.js" type="text/javascript">
</script>
no_longer_exist = '404' in sooup.find('script',type="text/javascript".get('src').split('/')
if no_longer_exist exist
    pass

203多进程爬虫的数据抓取

1.导入所需库

from multiprocessing import Pool

2.用函数填入页码

def get_all_links_from(channel):
    for num in range(1,101):
        get_links_form(channel.num)

3.创建进程池

if __name__ =='__main__'
    pool = Pool()
    pool.map(get_all_links_from,channel_list.split()

4.创建用来计数的监控程序

import time
while True:
    print(url_list.find().count();
    time.sleep(5)

204实战作业

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值