【问题标题】:ASCII Character in URL causing encoding debug warning in ScrapyURL中的ASCII字符导致Scrapy中的编码调试警告
【发布时间】:2014-07-03 20:26:16
【问题描述】:

下面,我有一个相当简单的 1 层爬虫供 Scrapy 执行。 它访问 Leis Municipais 数据库中针对圣保罗市为期 2 年(2012-2014 年)的搜索结果。 注意,起始 URL 包含 ASCII 格式的字符

大多数情况下,例如当 URL 中包含 ASCII 空格 (%20) 时,我只需使用带引号的字符串旁边的“u”段代码以 unicode 进行编码,这很有效(即,抓取或解析没有问题)。

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from lei_municipal.items import LeiMunicipalItem

class MySpider(CrawlSpider):
    name = "leis"
    allowed_domains = ["leismunicipais.com.br"]
    start_urls = [u"https://www.leismunicipais.com.br/cgi-local/forpgs/topsearch.pl?city=S%E3o%20Paulo&state=SP&tp=ord&page_this=1&block=0&year1=2012&year2=2014&ementaouintegra=naementa&wordkey=&&camara=1"]

    rules = (Rule (SgmlLinkExtractor(allow=(),restrict_xpaths=('//a[@class="pages_ant_prox"]',)), callback="parse_items", follow= True),)

    def parse_items(self, response):
        hxs = HtmlXPathSelector(response)
        leis = hxs.select('//div[@id="law_text"]')
        items = []
        for leis in leis:
            item = LeiMunicipalItem()
            item ["numero"] = leis.select("a/b/text()").extract()[0].encode("utf-8")
            item ["descricao"] = leis.select("a/div/text()").extract()[0].encode("utf-8")
            item ["url"] = leis.select("a/@href").extract()[0].encode("utf-8")
            items.append(item)
        return(items)

当我执行上述代码时,我收到以下 DEBUG 警告:

2014-07-03 17:15:01-0300 [leis] 调试:重定向(元刷新)到 https://www.leismunicipais.com.br> 来自 https://www.leismunicipais.com.br/cgi-local/forpgs/topsearch.pl?block=10&camara=1&city=S%C3%A3o+Paulo&ementaouintegra=naementa&page_this =2&state=SP&tp=ord&wordkey=&year1=2012&year2=2014> 2014-07-03 17:15:01-0300 [leis] 调试:重定向(元刷新)到 https://www.leismunicipais.com.br> 来自 https://www.leismunicipais.com.br/cgi-local/forpgs/topsearch.pl?block=490&camara=1&city=S%C3%A3o+Paulo&ementaouintegra=naementa&page_this =50&state=SP&tp=ord&wordkey=&year1=2012&year2=2014> 2014-07-03 17:15:02-0300 [leis] DEBUG: Crawled (200) https://www.leismunicipais.com.br> (referer: https://www.leismunicipais.com.br/cgi-local/forpgs/topsearch.pl?city=S%E3o%20Paulo&state=SP&tp=ord&page_this=1&block=0&year1=2012&year2=2014&ementaouintegra=naementa&wordkey=&&camara=1) 2014-07-03 17:15:02-0300 [leis] 信息:关闭蜘蛛(完成)

第一个建议是测试 xpaths

来自scrapy shell:

scrapy shell "https://www.leismunicipais.com.br/cgi-local/forpgs/topsearch.pl?city=S%E3o%20Paulo&state=SP&tp=ord&page_this=1&block=0&year1=2012&year2=2014&ementaouintegra=naementa&wordkey=&&camara=1"

测试第一个选择器:

hxs.select('//a[@class="pages_ant_prox"]/text()')

产量:

HtmlXPathSelector xpath='//a[@class="pages_ant_prox"]' data=u'a href="topsearch.pl?city=S%C3%A3o%20Pa'

HtmlXPathSelector xpath='//a[@class="pages_ant_prox"]' 数据=u'a href="topsearch.pl?city=S%C3%A3o%20Pa'

HtmlXPathSelector xpath='//a[@class="pages_ant_prox"]' 数据=u'a href="topsearch.pl?city=S%C3%A3o%20Pa'

HtmlXPathSelector xpath='//a[@class="pages_ant_prox"]' 数据=u'a href="topsearch.pl?city=S%C3%A3o%20Pa'

测试第二组选择器之一:

hxs.select('//div[@id="law_text"]/a/b/text()')

产量:

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16010/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16009/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16008/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16007/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16006/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16005/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16004/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16003/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16002/2014'

HtmlXPathSelector xpath='//div[@id="law_text"]/a/b/text()' data=u'LEI ORDIN\xc1RIA N\xba: 16001/2014'

我的问题是:为什么起始 URL 没有被正确编码,因此 scrapy 没有抓取和解析所需的信息?如何纠正?从我个人的故障排除来看,问题似乎是特定于“%e3”字符(~圣保罗的 ã)。

在 shell 中测试了选择器后,我更加困惑为什么当前脚本没有解析/显示所需的结果。

【问题讨论】:

    标签: python unicode ascii scrapy web-crawler


    【解决方案1】:

    不是网址的问题。该网站会进行元刷新,这意味着它们可能具有某种反爬虫机制。

    【讨论】:

    • 您能否提供任何链接,指向有关如何应对上述元刷新机制的信息? *** 我可能应该在上面的问题中提到我是这里的 Scrapy 新手!
    • 我仔细查看了该站点。它没有反爬虫机制(耶!)。我会重新检查链接提取器和内容提取器的 xpath。该网站的设计有点糟糕,例如所有法律容器都有相同的ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2021-11-01
    相关资源
    最近更新 更多