suchen

Heim  >  Fragen und Antworten  >  Hauptteil

python – Scrapy verwendet XPath, um Fehler auf Chinesisch zu melden

Problembeschreibung

links = sel.xpath('//i[contains(@title,"置顶")]/following-sibling::a/@href').extract()

Fehler: ValueError: Alle Zeichenfolgen müssen XML-kompatibel sein: Unicode oder ASCII, keine NULL-Bytes oder Steuerzeichen

大家讲道理大家讲道理2790 Tage vor1499

Antworte allen(2)Ich werde antworten

  • 学习ing

    学习ing2017-06-30 09:57:44

    参见文章:解决Scrapy中xpath用到中文报错问题

    解决方法

    方法一:将整个xpath语句转成Unicode

    links = sel.xpath(u'//i[contains(@title,"置顶")]/following-sibling::a/@href').extract()

    方法二:xpath语句用已转成Unicode的title变量

    title = u"置顶"
    links = sel.xpath('//i[contains(@title,"%s")]/following-sibling::a/@href' %(title)).extract()

    方法三:直接用xpath中变量语法($符号加变量名)$title, 传参title即可

    links = sel.xpath('//i[contains(@title,$title)]/following-sibling::a/@href', title="置顶").extract()

    Antwort
    0
  • ringa_lee

    ringa_lee2017-06-30 09:57:44

    整个字符串前加个u试试

    Antwort
    0
  • StornierenAntwort