Scrapy 解析 javascript [英] Scrapy parse javascript

查看:50
本文介绍了Scrapy 解析 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有一个 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)打印 ID

但我得到了:

exceptions.AttributeError: 'Selector' 对象没有属性 'search'

解决方案

Scrapy 选择器有 内置支持正则表达式:

sel.xpath('<xpath_to_find_the_element_text>').re(r'"id":(\d+)')

演示显示此特定正则表达式的工作:

<预><代码>>>>进口重新>>>s = 'new Shopify.OptionSelectors("product-select", { product: {"id":185310341,"title":"10. Design | Siyah \u0026 beyaz kalpli",'>>>re.search('"id":(\d+)', s).group(1)'185310341'

I have a javascript on the page like below:

new Shopify.OptionSelectors("product-select", { product: {"id":185310341,"title":"10. Design | Siyah \u0026 beyaz kalpli",

i want to get "185310341". I am searching on google about a few hours but couldn't find anything, I hope u can help me. How can i scrape that javascript and get that id?

I tried that code :

id = sel.search('"id":(.*?),',text).group(1)
print id

but i got:

exceptions.AttributeError: 'Selector' object has no attribute 'search'

解决方案

Scrapy selectors have built-in support for regular expressions:

sel.xpath('<xpath_to_find_the_element_text>').re(r'"id":(\d+)')

Demo showing the work of this particular regular expression:

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

这篇关于Scrapy 解析 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆