ScrapyはWebクローラーで、私はスパイダーを作成しました。 スパイダーは、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)
私はresponse.xpath( "// body")を試みました。extract()とそれは仕事をしませんでした。私はresponse.bodyの作品を知っていますが、私はxpathを学びたいと思います。 – user6658170
HTMLファイルに最終的にどのような結果が出るかについて質問を明確にするのに役立ちます。サーバーから返されたディスクにディスクを書き込むだけであれば、XPathはまったく必要ありません。 – Markus
私はhtmlファイルにbody elements.Onceが含まれるようにしたいので、私は特定のクラスを持つdiv要素をすべて集めるつもりです。 – user6658170