大佬教程收集整理的这篇文章主要介绍了web-crawler – scrapy如何用于提取网站的链接图?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有一种简单的方法来获得scrapy这样的图表?如果它可以更容易/更好地实现目标,我也会很高兴使用另一个开源工具.
>创建一个scrapy项目并生成一个默认的蜘蛛
$scrapy startproject sitegraph $cd sitegraph $scrapy genspider graphspider mydomain.com
>这将创建一个包含items.py文件的目录.在此文件中添加以下行
from scrapy.item import Item,Field class SitegraphItem(Item): url=Field() linkedurls=Field()
>在spiders目录中你会发现graphspider.py替换它(当然mydomain.com需要替换):
from scrapy.SELEctor import HtmlXPathSELEctor from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.contrib.spiders import CrawlSpider,Rule from scrapy.utilS.Url import urljoin_rfc from sitegraph.items import SitegraphItem class GraphspiderSpider(CrawlSpider): name = 'graphspider' allowed_domains = ['mydomain.com'] start_urls = ['http://mydomain/index.html'] rules = ( Rule(SgmlLinkExtractor(allow=r'/'),callBACk='parse_item',follow=TruE),) def parse_item(self,responsE): hxs = HtmlXPathSELEctor(responsE) i = SitegraphItem() I['url'] = response.url I['http_status'] = response.status llinks=[] for anchor in hxs.SELEct('//a[@href]'): href=anchor.SELEct('@href').extract()[0] if not href.lower().startswith("javascript"): llinks.append(urljoin_rfc(response.url,href)) I['linkedurls'] = llinks return i
>然后编辑setTings.py文件并添加(相应地更改文件名):
FEED_FORMAT="jsonlines" FEED_URI="file:///tmp/sitegraph.json"
>现在你可以运行:
$scrapy crawl graphspider
>这将生成一个json文件,您可以使用它来构建图形.
您可以使用像networkx这样的软件包来分析它的pygraphviz来绘制它(不建议用于大型网站)
import json import pygraphviz as pg def loadgraph(fName): G=pg.AGraph(directed=TruE) for line in open(fName): j=json.loads(linE) url=j["url"] G.add_node(url) for linked_url in j["linkedurls"]: G.add_edge(url,linked_url) return G if __name__=='__main__': G=loadgraph("/tmp/sitegraph.json") G.layout(prog='dot') G.draw("sitegraph.png")
以上是大佬教程为你收集整理的web-crawler – scrapy如何用于提取网站的链接图?全部内容,希望文章能够帮你解决web-crawler – scrapy如何用于提取网站的链接图?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。