【问题标题】:How toI crawl using scrapy to each one of the href如何使用scrapy抓取每个href
【发布时间】:2013-07-10 14:00:17
【问题描述】:

如何使用scrapy 抓取每个href?我只知道如何显示这一切,但我希望能够进入每个链接。这是我们的 Intranet 数据,因此您将无法访问这些链接。另外,当数据显示在文件中时,如何格式化日期?我需要在 start_url 中添加一个 url 列表吗?我需要将我的 initSpider 更改为 crawlSpider 吗?

<row>
<cell type="href" href="/dis/packages.jsp?view=list&show=perdevice&device_gid=6F5941585835587177572B3465656A61496B76747A673D3D54766B47446C376A77555A72624237756330506755673D3D&hwdid=353918053831794&mdn=14256238845&subscrbid=310260548400764&maxlength=100">14256238845</cell>
<cell type="href" href="/dis/packages.jsp?view=list&show=perdevice&device_gid=6F5941585835587177572B3465656A61496B76747A673D3D54766B47446C376A77555A72624237756330506755673D3D&hwdid=353918053831794&subscrbid=310260548400764&mdn=14256238845&maxlength=100">353918053831794</cell>
<cell type="href" href="/dis/packages.jsp?view=list&show=perdevice&device_gid=6F5941585835587177572B3465656A61496B76747A673D3D54766B47446C376A77555A72624237756330506755673D3D&hwdid=353918053831794&subscrbid=310260548400764&mdn=14256238845&maxlength=100">310260548400764</cell>
<cell type="href" href="/dis/packages.jsp?view=timeline&show=perdevice&device_gid=6F5941585835587177572B3465656A61496B76747A673D3D54766B47446C376A77555A72624237756330506755673D3D&hwdid=353918053831794&subscrbid=310260548400764&mdn=14256238845&maxlength=100&date=20130423T020032243">2013-04-23 02:00:32.243</cell>
<cell type="plain">2013-04-23 02:00:32.243</cell>
<cell type="plain">3 - PackageCreation</cell>
<cell type="href" href="/dis/profile_download?profileId=400006">400006</cell>
<cell type="href" href="/dis/sessions.jsp?view=list&device_gid=6F5941585835587177572B3465656A61496B76747A673D3D54766B47446C376A77555A72624237756330506755673D3D&hwdid=353918053831794&mdn=14256238845&subscrbid=310260548400764&maxlength=100">view sessions</cell>
<cell type="href" href="/dis/errors_agg.jsp?view=list&device_gid=6F5941585835587177572B3465656A61496B76747A673D3D54766B47446C376A77555A72624237756330506755673D3D&hwdid=353918053831794&mdn=14256238845&subscrbid=310260548400764&maxlength=100">view errors</cell>
</row>

这是我目前所拥有的,它可以打印所有内容

from scrapy.contrib.spiders.init import InitSpider
from scrapy.http import Request, FormRequest
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import Rule

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

from scrapy.selector import XmlXPathSelector

from carrier.items import CarrierItem

class CarrierSpider(InitSpider):
    name = 'dis'
    allowed_domains = ['qvpweb01.ciq.labs.att.com']
    login_page = 'https://qvpweb01.ciq.labs.att.com:8080/dis/login.jsp'
    start_urls = ["https://qvpweb01.ciq.labs.att.com:8080/dis/"]

    def init_request(self):
    #"""This function is called before crawling starts."""
    return Request(url=self.login_page, callback=self.login)

    def login(self, response):
    #"""Generate a login request."""
    return FormRequest.from_response(response,
            formdata={'txtUserName': 'myuser', 'txtPassword': 'xxxx'},
            callback=self.check_login_response)

    def check_login_response(self, response):
    #"""Check the response returned by a login request to see if we aresuccessfully logged in."""
    if "logout" in response.body:
        self.log("\n\n\nSuccessfully logged in. Let's start crawling!\n\n\n")
        # Now the crawling can begin..

        return self.initialized() 

    else:
        self.log("\n\n\nFailed, Bad password :(\n\n\n")
        # Something went wrong, we couldn't log in, so nothing happens.


    def parse(self, response):
    xhs = XmlXPathSelector(response)
    columns = xhs.select('//table[3]/row/cell')
    for column in columns:
        item = CarrierItem()
        item['title'] = column.select('.//text()').extract()
        item['link'] = column.select('.//@href').extract()
        yield item

我从下面的 csv 文件得到的输出:

14256238845
3.53918E+14
3.10261E+14
00:32.2
00:32.2
3 - PackageCreation
400006
view sessions
view errors

希望从 csv 得到的输出如下:

14256238845
353918053831794
310260548400764
2013-04-23 02:00:32.243
2013-04-23 02:00:32.243
3 - PackageCreation
400006
view sessions
view errors

【问题讨论】:

  • 你想要的输出是什么?不清楚你在问什么:是的,href 属性中有 url,你想关注它们,但你想从它们那里得到什么?
  • 嘿@alecxe 我将所需的输出和我在 csv 文件中获得的输出添加到我发布的问题中。另外我将如何关注每个链接。所有输出都是链接(14256238845、353918053831794、310260548400764、400006、查看会话、查看错误)。我想关注这些链接。唯一不是链接的是 (2013-04-23 02:00:32.243, 2013-04-23 02:00:32.243, 3 - PackageCreation)

标签: xml screen-scraping scrapy


【解决方案1】:

只要你想关注一个 URL,你就可以产生一个 Request 对象。
例如:yield Request(extracted_url_link, callback=your_parse_function)

查看以下链接中的第二个示例。
http://doc.scrapy.org/en/latest/topics/spiders.html#basespider-example

另一种指定抓取网址的方法是使用 SgmlLinkExtractor。你可以写规则。如果规则匹配,Spider 将抓取任何页面中的所有 url。请参考以下网址中的示例。
http://doc.scrapy.org/en/latest/topics/spiders.html#crawlspider

日期只是抓取后的一个字符串,你可以将它转换为python datetime对象,然后使用strftime等日期时间渲染函数以你想要的方式显示。

希望我回答了你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2015-03-06
    • 1970-01-01
    • 2013-03-08
    • 2012-10-26
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多