【问题标题】:Scrapy CSS selector for HTML element with class containing non alphanumeric char用于 HTML 元素的 Scrapy CSS 选择器,其类包含非字母数字字符
【发布时间】:2017-09-21 16:33:26
【问题描述】:

我正在使用 Scrapy 抓取网站。我正在尝试在响应中选择一个元素。

我的响应对象包含如下所示的 HTML:

<html>
    <head><title>Title goes here</title</head>
    <body>
         <!-- lots of stuff I am not interested in -->
         <select class="Gy(t)">
             <!-- elements -->
         </select>
         <!-- lost more stuff of no interest to me -->
    </body>
</html>

我的代码:

def parse(self, response):
    # ....
    print (response.selector.css('select.Gy(t)'))

我在运行代码时抛出了以下异常:

"Expected selector, got %s" % (peek,))
File "<string>", line None
cssselect.parser.SelectorSyntaxError: Expected selector, got <DELIM '(' at 9>

如何使用选择器(最好是 CSS 选择器)来选择这个 HTML 元素?

【问题讨论】:

    标签: python css scrapy scrapy-spider


    【解决方案1】:

    你可以转义css选择器上的括号,所以试试:

    response.css('select.Gy\(t\)')
    

    【讨论】:

    • 很高兴帮助 +1
    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 2016-01-02
    • 2018-04-09
    • 2015-12-22
    • 2021-12-13
    • 2016-10-29
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多