我在页面上有一个 javascript,如下所示:
new Shopify.OptionSelectors("product-select", { product: {"id":185310341,"title":"10. Design | Siyah \u0026 beyaz kalpli",
我想得到“185310341”。我在谷歌上搜索了大约几个小时,但找不到任何东西,我希望你能帮助我。我怎样才能抓取那个 javascript 并获得那个 id?
我试过那个代码:
id = sel.search('"id":(.*?),',text).group(1)
print id
但是我得到了:
exceptions.AttributeError: 'Selector' object has no attribute 'search'
最佳答案
Scrapy 选择器有 built-in support对于正则表达式:
sel.xpath('<xpath_to_find_the_element_text>').re(r'"id":(\d+)')
展示这个特定正则表达式的工作的演示:
>>> import re
>>> s = 'new Shopify.OptionSelectors("product-select", { product: {"id":185310341,"title":"10. Design | Siyah \u0026 beyaz kalpli",'
>>> re.search('"id":(\d+)', s).group(1)
'185310341'
关于python - Scrapy 解析 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23662069/