Scrapy是一个网络爬虫,我创建了一个蜘蛛。 我想要蜘蛛创建2个链接的正文2个HTML文件。 创建的html文件是空的。为什么这个xpath表达式不起作用?
import scrapy
from scrapy.selector import Selector
from scrapy.http import HtmlResponse
class DmozSpider(scrapy.Spider):
name = "dmoz"
allowed_domains = ["dmoz.org"]
start_urls = [
"http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
"http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
]
def parse(self, response):
x=response.xpath("//body/text()").extract()
filename = response.url.split("/")[-2] + '.html'
with open(filename, 'wb') as f:
f.write(x)
什么其他xpath表达式将工作复制body.I尝试response.xpath(“//body”)。extract()并没有工作。我知道response.body工作,但我想学习xpath。 – user6658170
这将有助于您澄清您的问题,以确定您最终会在HTML文件中达到什么目的。如果您只是希望将它们写入到服务器返回的光盘中,则完全不需要XPath。 – Markus
我希望html文件包含body元素。一旦有效,我将收集所有具有特定类的div元素。 – user6658170