如何在scrapy中提取javascript值 [英] How can I extract a javascript value in scrapy

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

问题描述

我正在使用 scrapy 来抓取 youtube 视频,我需要视频的标题/描述语言.当我在 这个视频 我可以在脚本标签内有一个变量 'METADATA_LANGUAGE': 'no'.我可以在scrapy及其扩展中提取这个值吗,或者我应该使用beautifulsoup/htmlparser之类的库下载和解析html.

I am using scrapy to crawl youtube videos and I need the language of title/description of the video.When I use browser view source on this video I can inside a script tag there is a variable 'METADATA_LANGUAGE': 'no'. Can I extract this value in scrapy and its extensions or I should download and parse html with libraries like beautifulsoup / htmlparser.

推荐答案

基于 this 可以选择文本使用 xpath/css 编写脚本,然后使用正则表达式搜索变量名称.假设第一个脚本包含 METADATA_LANGUAGE:

Based on this you can select the text of script with xpath/css and then use regex to search the variable name. Assum the first script contains the METADATA_LANGUAGE:

items = response.xpath('//script/text()')[0].re(".*METADATA_LANGUAGE.*")

这篇关于如何在scrapy中提取javascript值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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