我的第一个问题在这里:)尝试使用scrapy检索网页的所有链接。但我不能输出页面上的链接
我试图抓取我的学校网站的所有可能的网页有。但我无法将链接导入到文本文件中。我有正确的权限,所以这不是问题。
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from scrapy.item import Item
from scrapy.spider import BaseSpider
class hsleidenSpider(CrawlSpider):
name = "hsleiden1"
allowed_domains = ["hsleiden.nl"]
start_urls = ["http://hsleiden.nl"]
# allow=() is used to match all links
rules = [
Rule(SgmlLinkExtractor(allow=()), follow=True),
Rule(SgmlLinkExtractor(allow=()), callback='parse_item')
]
def parse_item(self, response):
x = HtmlXPathSelector(response)
filename = "hsleiden-output.txt"
open(filename, 'ab').write(response.url)
所以我只在hsleiden.nl页面上扫描。我想将response.url放入文本文件hsleiden-output.txt中。
有什么办法可以做到这一点吗?
请具体说明您的问题。你期望发生什么?发生了什么呢? – Sheena
如果我想借用你的代码,我怎么称呼它? –