我用Scrapy写了一个爬虫,用来获取dmoz.org上的一些东西。
当我在Python shell中使用response.xpath进行检查时,我得到了我想要的东西,但是当我在cmd中运行这个爬行器时,我没有得到任何东西。我很困惑。
下面是我的爬虫代码:
import scrapy
from kecheng3.items import Kecheng3Item
class DmozSpiderSpider(scrapy.Spider):
name = "dmoz_spider"
allowed_domains = ["dmoz.org"]
start_urls = ["http://www.dmoz.org/Computers/Programming/Languages/Python/Books/"]
def parse(self, response):
for divm in response.xpath('//*[@id="site-list-content"]/div'):
item = Kecheng3Item()
item['title'] = divm.xpath('/div[3]/a/div/text()').extract()
item['link'] = divm.xpath('/div[3]/a/@href').extract()
item['desc'] = divm.xpath('/div[3]/div/text()').extract()
yield item


发布于 2017-03-09 14:14:31
item['title'] = divm.xpath('./div[3]/a/div/text()').extract()
item['link'] = divm.xpath('./div[3]/a/@href').extract()
item['desc'] = divm.xpath('./div[3]/div/text()').extract()/表示根
./表示当前节点,在本例中是divm节点。
默认情况下,您可以这样做:
item['title'] = divm.xpath('div[3]/a/div/text()').extract()https://stackoverflow.com/questions/42687952
复制相似问题