简体   繁体   中英

Python xpath => SyntaxError: invalid syntax

I am a newbie to python. I'm trying to crawl some text, but it doesn't work... Here is my code

    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()

and this is the html code.

            <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>

Error message:

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

plz help me...T0T

You are missing ) after HtmlResponse(uurl="", body=a.encode('utf-8') it should be

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

instead of what you have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM