I am using simple_html_dom to scrape some webpages and I need help. Here is my Javascript:
<script type="text/javascript">
var session = "";
var order = "1";
var status = "";
var json_product = [{bla bla bla...}];
</script>
So I want to get inside var json_produk = ... is meaning:
[{"bla.... bla... bla... until }];
How can I get it?
I am trying for:
$html->find('var', 2);
I just get a 500 error because find use for find string I think.
I am using [an HTML parser] to scrape some webpages
But the value you want is in javascript (which is embedded in the HTML).
While you can retrieve the entire block of javascript with the DOM parser, that's as much resolution as it will give you.
$script=$html->find('script') . ''; // not tested - YMMV
PHP does not have a Javascript parser. If you can be sure that the JSON will always be assigned to "json_product" and that there is no further javascript code, then you could.....
$srch='json_product =';
$start=strpos($script, $srch);
$json=substr($script, $start+strlen($srch)+1);
$data=json_decode($json);
....but this is a long way from a robust solution.
I just get a 500 error because find use for find string I think.
That should not cause "a 500 error"
Related
Hey guys quick question what is the best approach on converting Model to Javascript.
I tried using this
#{
var arr = new string[3];
arr[0] = "1";
arr[1] = "2";
arr[2] = "3";
var jason = JsonConvert.SerializeObject(Model);
}
<script type="text/javascript">
var string = '#jason';
var variable2 = jQuery.parseJSON(string);
alert(variable2.Id);
</script>
as you can guess it did not work, I look through the sample of jQuery which should work but I am not sure if it is compatible with JsonConvert of .net
Its my first time on programming in the client side so this is quite a simple question but I tried looking the answer from the net. Mostly I see are uber complicated answers and at least 3 and higher old so I am guessing there should be an easier way to do this now?
I feel like this needs an answer because it's the number 2 result from google and has been viewed over 200 times, and as stated by the OP every other answer is generally long and/or convoluted. But the simplest answer is:
Javascript added to your aspx page:
<script type="text/javascript">
var myObject = <%=JsonConvert.SerializeObject(MyNetObject)%>;
</script>
The key bit being that your output of HTML looks as follows:
<script type="test/javascript">
var myObject = {"Prop1":"value1","Prop2":"Value2"};
</script>
Javascript natively handle JSON so you don't need to reparse anything on the client side. If the object is properly serialized it will be recognized without further conversion (this includes lists and object properties).
In the example provided in the OP the resultant Javascript object would be a simple array. So access to the elements on the client side would be:
alert(variable2[0]), alert(variable2[1]), alert(variable2[2])....
In order to provide the Javascript property calling functionality that OP is trying to do on the client side would require the following on the server side:
var json = JsonConvert.Serialize(new { ID1 = 1, ID2 = 2, ID3 = 3 });
In which an object with Named properties is serialized into the page.
In this case, Let's take Google as example:
The code is JScript .NET, which is basically a .NET version of Javascript.Regardless of language, Anyone with appending type of skill can answer my question
The problem is, when I keep issuing the request it keeps appending over and over again, http://i.imgur.com/nKCvgKn.png
if (oSession.uriContains("q="))
{
var str = oSession.fullUrl;
var sAppend = "+test1+test2+test3";
if (!oSession.uriContains(sAppend))
{
oSession.fullUrl = str.replace( "q=","q="+sAppend);
}
}
Thank you.
Notice: When I type from main page Google starts like search?site=&source=hp&q=&oq=, then after the first append it turns into search?q=&oq=
I am trying to implement server side code on client side. "trvddl1" is a ascx control which contains a dropdownlist "ddltree". It was easy on server side but I am facing difficulty using the same in javascript.
How do I write the following code in javascript?
((DropDownList)trvddl1.FindControl("ddltree")).SelectedValue;
I tried
var abc = document.getElementById('<%=trvddl1.ClientID%>').value;
and
var Region = document.getElementById('<%=trvddl1.FindControl("ddltree")%>').value;
but javascript returned error. Is there some other keyword I am missing ?
Check the HTML output (Browser-->View Source) and locate the control there, see what the ID of that control has, and put that one into the getElementById() function.
Example:
<input id='ddltree' .... />
Then use:
var abc = document.getElementById('ddltree').value;
Perhaps you can try something like that:
// find all controls that have an id that ends with ddltree
// starts with would be [id*=ddltree]
var abc = document.querySelectorAll("[id$=ddltree]");
if(abc.length > 0) {
// got it !
console.log(abc[0].value);
}
Please note that querySelectorAll is not supported in all browsers (even though - most). Here is a reference.
when i use user defined tags with uppercase node like "<ABC> test </ABC>" in ckeditor .On clicking source, it gets displayed as "<abc> test </abc>".please help me to get the expected output , which should be <ABC> test </ABC> and please guide me where the code should be modified.Thanking you
(Continued from comments) I propose post-processing the content and not trying to bend CKEditor to produce Case Sensitive output.
I don't know your languages or your architecture, but if you get the data from CKEditor with getData(), you can do something like this if you want to do the conversion in the client side:
// Javascript
var i = CKEDITOR.instances.editor1;
var d = i.getData();
var correctData = d.replace(/<abc/ig, '<ABC');
In the backend you can do something similar
// C# (untested)
string result = Regex.Replace(
htmlStringFromAJAX,
RegEx.Escape("<abc"),
RegEx.Escape("<ABC"),
RegexOptions.IgnoreCase
);
// PHP (untested)
$result = str_ireplace("<abc", "<ABC", $htmlStringFromAJAX);
(I hope you either have just this one abc tag or a small static amount of tags - if not, this will be a very annoying solution to maintain.)
I simply need to extract some info between two tags, in this case, <title>
For example:
...
<title>I Need This!</title>
...
And I simply need to be able to get the information between the tags. I was thinking using split(), however, I haven't been able to figure out how to cut all data before and after, and just catch the stuff in the title tags. As you can tell, I'm a beginner with text formatting. Thanks!
EDIT: An example of the type of file I'm looking through is here: https://gdata.youtube.com/feeds/api/videos/http://youtu.be/_OBlgSz8sSMg?v=2
I'm simply trying to take what's in the title tags to get the title of the video.
var text = '<title>I Need This!</title>',
match = text.match(/<title>([^<]*)<\/title>/),
youGotThis = match[1];
Here's the fiddle: http://jsfiddle.net/RPbSE/
DOM works on XML just as well as on HTML, so a simple
var titleNodeList = yourXMLDocument.getElementsByTagname('title');
var firstTitle = titleNodeList[0];
var titleTextNode = firstTitle.firstChild;
alert(titleTextNode);
should do.