知道创宇的爬虫面试题

使用python编写一个网站爬虫程序,支持参数如下:

spider.py -u url -d deep -f logfile -l loglevel(1-5) --testself -thread number --dbfile filepath --key=”HTML5”

参数说明
-u 指定爬虫开始地址
-d 指定爬虫深度
–thread 指定线程池大小,多线程爬取页面,可选参数,默认10
–dbfile 存放结果数据到指定的数据库(sqlite)文件中
–key 页面内的关键词,获取满足该关键词的网页,可选参数,默认为所有页面
-l 日志记录文件记录详细程度,数字越大记录越详细,可选参数,默认spider.log
–testself 程序自测,可选参数

功能描述
1、指定网站爬取指定深度的页面,将包含指定关键词的页面内容存放到sqlite3数据库文件中
2、程序每隔10秒在屏幕上打印进度信息
3、支持线程池机制,并发爬取网页
4、代码需要详尽的注释,自己需要深刻理解该程序所涉及到的各类知识点
5、需要自己实现线程池

提示
提示1:使用re urllib/urllib2 beautifulsoup/lxml2 threading optparse Queue sqlite3 logger doctest等模块
提示2:注意是“线程池”而不仅仅是多线程
提示3:爬取sina.com.cn两级深度要能正常结束

建议程序可分阶段,逐步完成编写,例如:
版本1: Spider1.py -u url -d deep
版本2:Spider3.py -u url -d deep -f logfile -l loglevel(1-5) --testself
版本3:Spider3.py -u url -d deep -f logfile -l loglevel(1-5) --testself -thread number
版本4:剩下所有功能

1.解析命令行模块optparse(optparse现在不再更新了,更新版本叫 argparse)

def parse():
    parser = OptionParser(
        description="This script is used to crawl analyzing web!")
    # 在用户请求帮助时打印它,并会格式化以适合当前终端宽度
    parser.add_option("-u", "-url", dest="urlpth", action="store",
                      help="Path you want to fetch", metavar="www.sina.com.cn")
    parser.add_option("-d", "-deep", dest="depth", action="store", type="int",
                      help="Url path's deep, default 1", default=1)
    parser.add_option("-k", "-key", dest="key", action="store",
                      help="You want to query keywords, For example 'test'")
    parser.add_option("-f", "-file", dest="logfile", action="store",
                      help="Record log file path and name, default spider.log",
                      default='spider.log'),
    parser.add_option("-l", "-level", dest="loglevel", action="store",
                      type="int", help="Log file level, default 1(CRITICAL)",
                      default=1),
    parser.add_option("-t", "-thread", dest="thread", action="store",
                      type="int", help="Specify the thread pool, default 10",
                      default=10),
    parser.add_option("-q", "-dbfile", dest="dbpth", action="store",
                      help="Specify the the sqlite file directory and name, \
                  default  test.db", metavar='test.db')
    parser.add_option("-s", "-testself", dest="testself", action="store_true",
                      help="Test myself", default=False)
    (options, args) = parser.parse_args()
    return options

基本用法

  1. 载入OptionParser类,新建对象: OptionParser()
  2. 添加选项: add_option(…)
  3. 参数解析: parse_args() 默认是使用 sys.argv[1:] 作为参数, 也可以传递一个命令行参数列表: parse_args(list).

add_option方法参数含义

  • dest:用于保存输入的临时变量,其值通过options的属性访问
  • help:用于生成帮助信息
  • action:指导程序遇到命令行参数怎么样处理,三种值可选。
    • store:读取参数,如果参数符合type要求,则将值传递给dest变量。强制要求后面提供参数
    • store_true/store_false: 作为标记使用,设置dest变量的值为True或False。后面可以不跟参
  • metavar:在打印帮助文本时使用的选项参数的替代值。
  • default:给dest默认值
  • type:检查命令行参数数据类型,有string,int,float等

parse_args():

  • options:它是一个对象,保存有命令行参数值。只要知道命令行参数名,如 input,就可以访问其对应的值:options.input
  • args:它是没被解析的命令行参数的列表。

参考链接:
https://www.jianshu.com/p/fef2d215b91d 【简单易懂,argparse模块】
https://www.jianshu.com/p/bec089061742 【详解optparse模块】
https://docs.python.org/3.7/library/optparse.html#optparse-tutorial【官网】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值