【问题标题】:Python xpath => SyntaxError: invalid syntaxPython xpath => SyntaxError:无效语法
【发布时间】:2017-10-05 08:58:07
【问题描述】:

我是 python 的新手。我正在尝试抓取一些文本,但它不起作用...... 这是我的代码

    def parse(self,response):

    hxs = Selector(response)
    article_list = hsx.xpath('//*[@id="weather"]')

    print('##################### total_length : ' + str(len(article_list)))

    for a in article_list:
        html_response = HtmlResponse(uurl="", body=a.encode('utf-8') #??
        seoul = html_response.xpath('//dl[@class="po_seoul"]//dd[@class="temp"]')[0].extract()

这是html代码。

            <h1 class="blind">weather_data</h1>
            <div id="weather" class="weather" >
                <dl class="po_seoul">
                    <dt>seoul</dt>
                    <dd class="weather"><a href="#" onclick="showLandForecast('1159068000'); return false;" data-stn="108"><img src="/images/icon/NW/NB04.png" alt="cloud" /></a></dd>
                    <dt class="blind">temperature</dt>
                    <dd class="temp">21.2</dd>
                </dl>

错误信息:

seoul = html_response.xpath('//dl[@class="po_seoul"]//dd[@class="temp"]')[0].extract() ^ SyntaxError: invalid syntax

请帮帮我...T0T

【问题讨论】:

  • html_response = HtmlResponse(uurl="", body=a.encode('utf-8') 您在这一行中缺少右括号。
  • 我刚刚修复了它,它可以工作了!谢谢你:D!

标签: python xpath web-crawler


【解决方案1】:

你在HtmlResponse(uurl="", body=a.encode('utf-8')之后缺少)应该是

HtmlResponse(uurl="", body=a.encode('utf-8'))
                                            ^

而不是你所拥有的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-23
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多