Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Android和iOS上反应本机获取html的不同结果_Javascript_React Native_Web Scraping_Fetch_Cheerio - Fatal编程技术网

Javascript 在Android和iOS上反应本机获取html的不同结果

Javascript 在Android和iOS上反应本机获取html的不同结果,javascript,react-native,web-scraping,fetch,cheerio,Javascript,React Native,Web Scraping,Fetch,Cheerio,有人能给我解释一下为什么我在安卓系统上做测试和在iphone上做测试时fetch的响应是不同的吗?这导致我在提取cheerio库使用的文本时出现问题 在安卓系统中,我可以完美地提取iPhone上没有的数据,我做错了什么,或者我失去了什么 非常感谢您的明智回应 代码: import React,{Component}来自“React”; 从“react native”导入{TextInput,Button}; 从“本机基”导入{View}; 导出默认函数SearchLinkText(){ cons

有人能给我解释一下为什么我在安卓系统上做测试和在iphone上做测试时fetch的响应是不同的吗?这导致我在提取cheerio库使用的文本时出现问题

在安卓系统中,我可以完美地提取iPhone上没有的数据,我做错了什么,或者我失去了什么

非常感谢您的明智回应

代码:

import React,{Component}来自“React”;
从“react native”导入{TextInput,Button};
从“本机基”导入{View};
导出默认函数SearchLinkText(){
const[value,onChangeText]=React.useState(“”);
const cheerio=要求(“反应本机cheerio”);
异步函数loadHTML(){
常量搜索URL=
"https://www.amazon.es/Bear-Village%C2%AE-Billetera Magn%C3%A9tico可折叠/dp/B0834Z8V2V/ref=sr_1_8?
__mk_es_es=%C3%85M%C3%85%C5%BD%C3%95%C3%91&keywords=funda+galaxy&qid=1579629120&s=kitchen&sr=1-8”;
const response=等待获取(searchUrl);//获取页面
const htmlString=wait response.text();//获取响应文本
const$=cheerio.load(htmlString,{decodeEntities:false});//解析HTML字符串
log('Full HTML:'+htmlString)
console.log(
“标题:”+
$(“h1.a-size-large span.a-size-large”)
.html()
.trim()
);
console.log(
“价格:”+
$(“分区a-截面分区特征跨度a-尺寸-中等a-颜色-价格”)
.html()
.trim()
);
}
返回(
onChangeText(文本)}
value={value}
/>
);
}
Android响应(Console.log):

log('Full HTML:'+htmlString)前30行:


变量ue_t0=ue_t0 | |+新日期();
window.ue|ihb=(window.ue|ihb | window.ueinit | 0)+1;
如果(window.ue_ihb==1){
变量ue_hob=+新日期();
变量ue_id='3K2TJQ58BVNQN3K7EHVJ',
ue_csm=窗口,
ue_err_chan='jserr rw',
ue={};
(d)函数(d)的(d)函数(b)的(d)的(d)的(b)的(b)的(b)的(b)的(b)的(b)的(b)的(b)的(d)的(b)的(d)的(b)的(d)的(d)的)的(d)的(d)的(b)的)的(d)的(b)的(b)的)的(b)的(b)的(b)的(d)的(b)的)的(b)的(b)的(b)的(b)的)的(b)的(b)的(b)的(b)的(b)的(b)的(b)的)的(b)的(b)的(b)的(b)的)的(b)的(b)的(b)的(b)的(b)的(b)的(b)的)的(b)的(b)的)的)的(1};e.exec=函数(b,a){return function(){try{return b.apply(this,arguments)}catch(c){ueLogError(c,{属性:a | | | |“未定义”,logLevel:“WARN”}}}}})(ue|csm);
ue.stub(ue,“log”);ue.stub(ue,“onnload”);ue.stub(ue,“onflush”);
(d,e)的功能(d,e)的功能(d,e)的功能(d,e)的功能(d,e)的功能(d,e)的功能(d,e)的功能(d,e)的功能(d,e)的功能(d,e)的功能(f,b)的功能(f,b)的功能(f)的功能(f,b)的功能(f(f,b)和(f)的功能(f)和(f)和(f)的功能(f)和(d)的(d)的功能(d(d,d,d,e)的功能(d,e)c)c)c=c=c=c=f.c)的c=f.c=f.c=f.LOG水平(c=f.c=f.c=f.log水平(f.c=f.c=f.c=f.LOG水平(f.c)的功能(f(f.c)的(f(f)的(f.f push({ex:f,info:b}}}函数l(a,b,c,e,g){d.ueLogError({m:a,f:b,l:c,c:“+e、 err:g,fromOnError:1,args:arguments},g{attribute:g.attribute,logLevel:g.logLevel}:void 0);return!1}var k=“FATAL”,m=“ERROR”,n=“WARN”,p=“降级”,a={ec:0,ecf:0,
pec:0,ts:0,erl:[],ter:[],mxe:50,startTimer:function(){a.ts++;setInterval(function(){d.ue&&a.pec
亚马逊
熊村┬«Funda para三星Galaxy S9 Plus,Billetera Carcasa de Cuero con TPU Tapa Interna,Cierre Magn├®提科、索波特可折叠、蓝色



这里可能发生的两件事,在我的脑海中,可能是你正在请求的Amazon页面上遇到AB测试,或者请求头在不同平台上不同,因此Amazon基于其中一个平台提供不同版本的页面。第一点被丢弃,因为我做了无数次测试,所有的测试都是s因此,第二点似乎是最有效的,但我更倾向于推断它可能与iOS可能采用的某些安全策略有关have@jasonmerino在阅读了你的分析之后,我立即对一个随机站点进行了测试,结果是相同的,这意味着你是对的,出于某种原因,亚马逊提供了2个di不同的版本,问题在于iOS版本与Android版本不同,它极其“差且基本”,缺少有效的标签和样式,这使得我的目标无法实现。关于为什么会发生这种情况以及如何解决这种情况的任何假设?至于为什么会发生这种情况,可能是某种程度上的商业决策,以使响应不同。可能与样式相关,可能是他们希望显示完全不同的内容(可能是在某个特定平台上宣传一些只对用户重要的东西)。至于解决这个问题,你可以尝试将Android请求中默认发送的所有请求头硬编码到你的
fetch
调用中,看看它是否会在iOS上返回与Android相同的响应。
import React, { Component } from "react";
import { TextInput, Button } from "react-native";
import { View } from "native-base";

export default function SearchLinkText() {
  const [value, onChangeText] = React.useState("");
  const cheerio = require("react-native-cheerio");

  async function loadHTML() {
    const searchUrl =
      "https://www.amazon.es/Bear-Village%C2%AE-Billetera-Magn%C3%A9tico-Plegable/dp/B0834Z8V2V/ref=sr_1_8?
__mk_es_ES=%C3%85M%C3%85%C5%BD%C3%95%C3%91&keywords=funda+galaxy&qid=1579629120&s=kitchen&sr=1-8";
    const response = await fetch(searchUrl); // fetch page


    const htmlString = await response.text(); // get response text

    const $ = cheerio.load(htmlString, { decodeEntities: false }); // parse HTML string

    console.log('Full HTML:' + htmlString)

    console.log(
      "Title: " +
        $("h1.a-size-large span.a-size-large")
          .html()
          .trim()
    );
    console.log(
      "Price: " +
        $("div.a-section div.feature span.a-size-medium.a-color-price")
          .html()
          .trim()
    );
  }

  return (
    <View
      style={{
        flexDirection: "row",
        justifyContent: "space-between",
        margin: 20
      }}
    >
      <TextInput
        style={{ height: 40, borderColor: "gray", borderWidth: 1, width: 250 }}
        placeholder="Paste Link"
        onChangeText={text => onChangeText(text)}
        value={value}
      />
      <Button title="Get" onPress={loadHTML} />
    </View>
  );
}
<!doctype html><html class="a-no-js" data-19ax5a9jf="dingo">
    <head>
<script type="text/javascript">var ue_t0=ue_t0||+new Date();</script>
<script type="text/javascript">
window.ue_ihb = (window.ue_ihb || window.ueinit || 0) + 1;
if (window.ue_ihb === 1) {
var ue_hob=+new Date();
var ue_id='3K2TJQ58BVNQN3K7EHVJ',
ue_csm = window,
ue_err_chan = 'jserr-rw',
ue = {};
(function(d){var e=d.ue=d.ue||{},f=Date.now||function(){return+new Date};e.d=function(b){return f()-(b?0:d.ue_t0)};e.stub=function(b,a){if(!b[a]){var c=[];b[a]=function(){c.push([c.slice.call(arguments),e.d(),d.ue_id])};b[a].replay=function(b){for(var a;a=c.shift();)b(a[0],a[1],a[2])};b[a].isStub=1}};e.exec=function(b,a){return function(){try{return b.apply(this,arguments)}catch(c){ueLogError(c,{attribution:a||"undefined",logLevel:"WARN"})}}}})(ue_csm);

ue.stub(ue,"log");ue.stub(ue,"onunload");ue.stub(ue,"onflush");

(function(d,e){function h(f,b){if(!(a.ec>a.mxe)&&f){a.ter.push(f);b=b||{};var c=f.logLevel||b.logLevel;c&&c!==k&&c!==m&&c!==n&&c!==p||a.ec++;c&&c!=k||a.ecf++;b.pageURL=""+(e.location?e.location.href:"");b.logLevel=c;b.attribution=f.attribution||b.attribution;a.erl.push({ex:f,info:b})}}function l(a,b,c,e,g){d.ueLogError({m:a,f:b,l:c,c:""+e,err:g,fromOnError:1,args:arguments},g?{attribution:g.attribution,logLevel:g.logLevel}:void 0);return!1}var k="FATAL",m="ERROR",n="WARN",p="DOWNGRADED",a={ec:0,ecf:0,
pec:0,ts:0,erl:[],ter:[],mxe:50,startTimer:function(){a.ts++;setInterval(function(){d.ue&&a.pec<a.ec&&d.uex("at");a.pec=a.ec},1E4)}};l.skipTrace=1;h.skipTrace=1;h.isStub=1;d.ueLogError=h;d.ue_err=a;e.onerror=l})(ue_csm,window);

ue.stub(ue,"event");ue.stub(ue,"onSushiUnload");ue.stub(ue,"onSushiFlush");

var ue_url='/Bear-Village%C2%AE-Billetera-Magn%C3%A9tico-Plegable/dp/B0834Z8V2V/ref=sr_1_8/uedata/unsticky/262-7660088-5537506/NoPageType/ntpoffrw',
ue_sid='262-7660088-5537506',
ue_mid='A1RKKUPIHCS9HS',
ue_sn='www.amazon.es',
ue_furl='fls-eu.amazon.es',
ue_surl='https://unagi-eu.amazon.com/1/events/com.amazon.csm.nexusclient.prod',
ue_navtiming=1,
ue_fcsn=1,
ue_isrw=true,
ue_fpf='//fls-eu.amazon.es/1/batch/1/OP/A1RKKUPIHCS9HS:262-7660088-5537506:3K2TJQ58BVNQN3K7EHVJ$uedata=s:',
ue_qsl=2000,
<!DOCTYPE html>
<html dir=ltr >
<head>
<title>Bear Village┬« Funda para Samsung Galaxy S9 Plus, Billetera Carcasa de Cuero con TPU Tapa Interna, Cierre Magn├®tico, Soporte Plegable, Azul:Amazon</title>
<link rel="canonical" href="https://www.amazon.es/Bear-Village%C2%AE-Billetera-Magn%C3%A9tico-Plegable/dp/B0834Z8V2V" />
<meta content="Bear Village┬« Funda para Samsung Galaxy S9 Plus, Billetera Carcasa de Cuero con TPU Tapa Interna, Cierre Magn├®tico, Soporte Plegable, Azul,Bear Village" name="keywords" /><meta content="Amazon Bear Village┬« Funda para Samsung Galaxy S9 Plus, Billetera Carcasa de Cuero con TPU Tapa Interna, Cierre Magn├®tico, Soporte Plegable, Azul" name="description" />
</head>
<body>
<a name="top"></a>
<center>Amazon.es</center>
<b id="product-title">Bear Village┬« Funda para Samsung Galaxy S9 Plus, Billetera Carcasa de Cuero con TPU Tapa Interna, Cierre Magn├®tico, Soporte Plegable, Azul</b>
<br />
<center>
<a href="/gp/aw/d/B0834Z8V2V/ref=mw_dp_img?is=m&qid=1579629120&sr=1-8&vs=1"><img src="https://images-na.ssl-images-amazon.com/images/I/71FVhkVbb1L._SL110_.jpg" width="110" id="detailImg" height="110" border="0" /></a>
<br />
<font size="-1">
<a href="/gp/aw/d/B0834Z8V2V/ref=mw_dp_img_2?in=2&qid=1579629120&sr=1-8&vs=1">Más imágenes</a>
</font>
</center>
<form method="post"
id="addToCart8"
action="/gp/aw/c/ref=mw_dp_buy_pre"
class="a-nostyle button-form">
<input type="hidden" name="a" value="B0834Z8V2V" /><input type="hidden" name="o" value="add" /><input type="hidden" name="oid" value="cBUmTruT542F5hkooRpkmtAVtdu3nKf5%2B1E%2BL4odyuwVf3sPoVEX2B1z9QZMxup6GU1URiR4yogV7Zb8zow0r9EWiLbaWYclqxMECJLW9hRrg38qlbpYuIs7KCpKNs%2FAV3IEndaiNnJC%2BUSwIBtYaITk8udEksGz" /><input type="hidden" name="qid" value="1579629120" /><input type="hidden" name="sr" value="1-8" /><input type="hidden" name="verificationSessionID" value="258-0382502-3749862" />
<center>
<input
name="addToCart"
class="cartButton"
type="submit"
value="A├▒adir preventa a la cesta"