简体   繁体   English

如何使用JavaScript单击图像按钮?

[英]How to click an image button using Javascript?

I am trying to crawl a multilanguage opencart website, which doesn't have direct link to its other language versions, which I need because I want to crawl it in all available languages. 我正在尝试抓取多语言的opencart网站,该网站没有直接链接到其其他语言版本,我需要这样做,因为我想以所有可用语言抓取它。 And it has set of javascript buttons that switch between the languages without changing the url. 它具有一组javascript按钮,可在不更改url的情况下在语言之间切换。 But when I access this website via crawler, it gives me only one language. 但是,当我通过搜寻器访问此网站时,它只给我一种语言。

For crawling I use Scrapy in conjunction with Splash, so to my understanding I need to pass a piece of javascript code to my crawler to click button to change the language, and only then start to render the webpage in the language I need, for further parsing. 对于抓取,我将Scrapy与Splash结合使用,因此据我所知,我需要将一段javascript代码传递给我的抓取器,以单击按钮以更改语言,然后才开始以我需要的语言呈现网页,以供进一步使用解析。

Here's the buttons code: 这是按钮代码:

<div id="language" class="lang-opacity">
    <img class="lang-active" src="http://example.com/catalog/view/theme/theme_name/image/flags/ru.png" alt="Русский" title="Русский" onclick="$('input[name=\'language_code\']').attr('value', 'ru'); $(this).parent().parent().submit();">
    <img src="http://example.com/catalog/view/theme/theme_name/image/flags/ua.png" alt="Украинский" title="Украинский" onclick="$('input[name=\'language_code\']').attr('value', 'ua'); $(this).parent().parent().submit();">
    <img src="http://example.com/catalog/view/theme/theme_name/image/flags/en.png" alt="English" title="English" onclick="$('input[name=\'language_code\']').attr('value', 'en'); $(this).parent().parent().submit();">
    <input name="language_code" value="" type="hidden">
    <input name="redirect" value="http://example.com/" type="hidden">
</div>

here's the code of function itself: 这是功能本身的代码:

function onclick(event) {
  $('input[name=\'language_code\']').attr('value', 'ru');
  $(this).parent().parent().submit();
}

So what kind of code should I pass to my crawler to switch a language for me? 那我应该传递给搜寻器什么样的代码来为我切换语言呢?

Thanks. 谢谢。

«what kind of code should I pass to my crawler» «我应该将哪种代码传递给我的搜寻器»

I assume you will pass it via the browser's console. 我假设您将通过浏览器的控制台传递它。 Try: 尝试:

$("input[name='language_code']").val("ru").closest("form").submit();

And just replace ru for the other language you need... 只需将ru替换为您需要的其他语言即可...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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