好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙。接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS。

背景

一般爬虫的逻辑是这样的,给定一个初始链接,把该链接的网页下载保存,接着分析页面中的链接,找到目标链接检查是否已经请求过,如果未请求则放入请求队列,页面下载完成后交给索引器建立索引,如此往复即可建立一套提供给搜索引擎使用的文档库。我当时的需求并不是这样,而是抓取某几个网站的数据并把规定的字段输出为结构化的文件最终会放到EXCEL中分析。后者也许只需要该网站全量的商品数据,其它一概不需要,这样见面不需要保存。

 

我将顺着时间顺序记录问题和想法。

  1. 编码
  2. 重定向
  3. 并发

问题

除了上篇遇到的编码问题,重定向问题在NodeJS中也比较麻烦,原因是它并不像在Python中的库一样支持自动重定向,需要开发者自己处理。在HTTP协议头中重定向的状态码:301, 302, 303, 307[1]

10.3.2 301 Moved Permanently

The new permanent URI SHOULD be given by the Location field in the response.

一旦发现需要重定向,我们必须读取HTTP response头中的Location属性,并对该URI发出request,需要注意的问题是该属性并不总是完整URI资源,也会遇到类似“/users.php”只包含PATH的值。

这样在代码中进行相应处理就没问题了,后来接触到了Mikeal Rogersrequest库,一切都变得简单很多,它默认自动处理重定向,还支持手动关闭。Cheers!

 

[1]. http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

posted @ 2015-05-17 19:05 月窟仙人 阅读(663) 评论(0) 推荐(0)
摘要: 好久没写博客了,这段时间已经忙成狗,半年时间就这么没了,必须得做一下总结否则白忙。接下去可能会有一系列的总结,都是关于定向爬虫(干了好几个月后才知道这个名词)的构建方法,实现平台是Node.JS。 背景 一般爬虫的逻辑是这样的,给定一个初始链接,把该链接的网页下载保存,接着分析页面中的链接,找到目标 阅读全文
posted @ 2015-05-17 18:32 月窟仙人 阅读(1320) 评论(2) 推荐(4)
摘要: 题目:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the". 1 public String reverseWor... 阅读全文
posted @ 2014-07-12 15:42 月窟仙人 阅读(137) 评论(0) 推荐(0)
摘要: 题目:Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number1 阅读全文
posted @ 2013-12-18 15:05 月窟仙人 阅读(158) 评论(0) 推荐(0)
摘要: 题目:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0这个题目比较简单,也就是二分作 阅读全文
posted @ 2013-12-18 14:18 月窟仙人 阅读(151) 评论(0) 推荐(0)
摘要: 题目:Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:boo 阅读全文
posted @ 2013-12-17 21:44 月窟仙人 阅读(217) 评论(0) 推荐(0)
摘要: 题目:Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 阅读全文
posted @ 2013-12-16 15:41 月窟仙人 阅读(162) 评论(0) 推荐(0)
摘要: 题目:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gray cod 阅读全文
posted @ 2013-12-15 16:43 月窟仙人 阅读(164) 评论(0) 推荐(0)
摘要: 题目:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it 阅读全文
posted @ 2013-12-15 15:32 月窟仙人 阅读(140) 评论(0) 推荐(0)
摘要: 题目:Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.开始理解题目的点问题,以为是漏掉的一个数,其实应该是第一个漏掉的自然数,这里自然数当然不包括0。所以只要从1开始每次加一看哪一个数没有出现在列表中就得到结果了,关键是怎么依次查呢?当时没想到办法,后来有 阅读全文
posted @ 2013-12-12 18:37 月窟仙人 阅读(174) 评论(0) 推荐(0)
点击右上角即可分享
微信分享提示