在Scrapy的旅行道路

时间:2014-05-30 17:26:23

标签: python scrapy

我正在尝试进行网页抓取,我遇到了Python/Scrapy的一些问题。 我已经隔离了我想要去的链接,但无法弄清楚如何去那里刮取更多数据。到目前为止我所拥有的是这样的:

def parse_site(self, response):
    sel = Selector(response)
    sites = sel.xpath('//a')

    # This part works and is responsible for getting only the links I want
    sites = [site for site in sites if "." in str(site.xpath('text()').extract())]
    items = []
    for site in sites:
        item = DomainManagerItem()

        dName = str(site.xpath('text()').extract())[3:-2]
        item['domainName'] = dName

此时我想将下一页的信息存储到项目的第二个字段中。我正在尝试执行以下操作。我将基地址存储在BASE_ADDRESS中,并使用newPath = str(site.xpath("@href").extract())来提取地址的第二部分,如果我print输出BASE_ADDRESS + newPath,则可以使用 item['totalUsers'] = self.parse_client(ResponseObj) 正是我在寻找的东西。我现在正在做的是尝试使用另一个模块从下一页获得一些东西。但是,我无法让它发挥作用。它看起来像这样。

totalUsers

我不确定如何获得正确的响应对象并尝试了许多不同的东西。虽然不能让它工作。我想我可以解析下一个文件并从中获取{{1}},这只是将它发送到下一个模块的问题。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

事实证明,有些事情我对Python并不了解并试图掌握scrapy。这是我使用的工作解决方案..

def parse_site(self, response):
    global BASE_WEBSITE
    sel = Selector(response)
    sites = sel.xpath('//a') # xpath for the sites
    # To get the desc. of the <a> tag use sel.xpath('//a/text()').extract()
    # Get all the websites that would lead to clients.
    sites = [site for site in sites if "." in str(site.xpath('text()').extract())]
   # items = []
    for site in sites:
        item = DomainManagerItem()

        # Get the Description and trim it
        dName = str(site.xpath('text()').extract())[3:-2]

        # Get the Path and trim it
        newPath = "https://" +  BASE_WEBSITE +  (str(site.xpath("@href").extract())[3:-2])

        item['domainName'] = dName
        yield Request(url = newPath, callback = self.parse_client, meta = {'item':item})


def parse_client(self, response):
    sel = Selector(response)
    ite = response.meta['item']
    site = sel.xpath('//td')
    ite['totalUsers'] = str(site[8].xpath('text()').extract())[3:-2]

    return ite
相关问题