|
|
@@ -27,6 +27,7 @@ class ApiSpider(scrapy.spiders.Spider):
|
|
|
#allUrl=response.url
|
|
|
#print allUrl
|
|
|
|
|
|
+ '''
|
|
|
#从response中获取内容,保存到数据库,为什么能保存到数据库呢?应该是架构写好的。
|
|
|
#extract方法返回unicode字符串
|
|
|
apiItem=ApiItem()
|
|
|
@@ -58,12 +59,12 @@ class ApiSpider(scrapy.spiders.Spider):
|
|
|
apiItem['content']=_content[0].extract() if _content else ""
|
|
|
|
|
|
yield apiItem
|
|
|
-
|
|
|
- # 下面的正则会提取页面中所有的后缀名为.aspx,.html .htm的超级链接,然后循环递归
|
|
|
+ '''
|
|
|
+ '''
|
|
|
+ # 下面的正则会提取页面中所有的后缀名为.aspx,.html .htm的超级链接,然后循环递归,但此方法会忽略掉没有"http://"前缀的网页
|
|
|
pattern=re.compile(r'((((https|http):\/\/)|\/)[0-9a-zA-Z\/\.@-_%]*?\.(aspx|html|htm))')
|
|
|
urls=pattern.findall(response.text)
|
|
|
for url in urls:
|
|
|
-
|
|
|
#去除重复
|
|
|
findUrl=url[0]
|
|
|
if findUrl in self.URLS:
|
|
|
@@ -73,6 +74,34 @@ class ApiSpider(scrapy.spiders.Spider):
|
|
|
#Request对象需要包含from scrapy.http import Request
|
|
|
#递归获取
|
|
|
yield Request(findUrl,callback=self.parse)
|
|
|
+ '''
|
|
|
+
|
|
|
+
|
|
|
+ #下面的会把a标签中的所有超级链接截取出来。得到的结果为一个selectorlist类型,结果更加准确。
|
|
|
+ _hrefs=response.xpath('//a/@href')
|
|
|
+ for href in _hrefs:
|
|
|
+ #获取a标签中的超级链接
|
|
|
+ _findurl=href.extract()
|
|
|
+
|
|
|
+ #http://api.1473.cn/DevelopInterfaces/1473/common/index.aspx/common/606UploadFiles.aspx/common/600CommonIntroduces.aspx
|
|
|
+ #结果会出现上面的重复累加的情况,再加一个判断试一试。这样有几率会误报
|
|
|
+ if response.url.find(_findurl) != -1:
|
|
|
+ continue
|
|
|
+
|
|
|
+ #判断超级链接是否包含"http"字符串,如果没有包含,则添加浏览器路径拼凑成完整的url
|
|
|
+ if _findurl.find('http') == -1:
|
|
|
+ _findurl=response.url+'/'+_findurl
|
|
|
+ #去除重复数据
|
|
|
+ if _findurl in self.URLS:
|
|
|
+ continue
|
|
|
+ else:
|
|
|
+ self.URLS.append(_findurl)
|
|
|
+
|
|
|
+ print _findurl;
|
|
|
+ #Request对象需要包含from scrapy.http import Request
|
|
|
+ #递归获取
|
|
|
+ yield Request(_findurl,callback=self.parse)
|
|
|
+
|
|
|
|
|
|
#filename=response.url.split("/")[2]
|
|
|
#with open(filename,'wb') as f:
|