Copy and paste text from a third party web page - javascript

I was wondering if there is a way to copy and paste some part of the text from a third party web page. My boss asked me to enter a group of text (50, 100, 200) one-by-one into this website: http://fbatoolkit.com/chart_details?category=T2ZmaWNlIFByb2R1Y3Rz&rank=500 and copy/paste the information "3 (30 days avg)" into another file. The "rank=500" part is the query string in the url. And I also know where the info, in the html source code, is. It is here:
<div style="margin: 20px">
Estimate sales per day for the rank
<input type="text" name="rank" value="500" />
in this category.
<input type="submit" value="Estimate" />
<table width="200">
<tr>
<td>
3 (30 Days Avg)
</d>
</tr>
<tr>
<td>
More than 2 (Last Day)
</td>
</tr>
</table>
</div>
</form>
I was wondering if there is a way to recursively access the website and copy/paste that part of text into another file. I know it is probably not the smartest way to do things but please help, the almighty stack overflow! I really appreciate that.

So I don't write python but I'll give it a shot. These types of tasks are usually very easy to accomplish with Python. So, I'll give you the general language constructs that I would use complete with links to accomplish this.
General Steps
Set up array of categories
Set up array of ranks to use
For loop through each category and then nested loop through each rank
Within this inner loop, query the web page like this: see This Answer for more options to opening and reading URLS
page = urllib.request.urlopen("URL HERE").read()
Then use RegEx to find the text you're interested in, by doing something like this (Note - the below RegEx was created assuming "(30 Days Avg)" was a static string, which it seemed like from page you supplied. You can re-append this text to the end of the grouped item if you'd like):
match = re.search("(\w+) (30 Days Avg)$", string)
extractedText = match.groups(0)
Append text to file of your choice per This Answer
Close out your loops
Sorry this wasn't more cut-and-paste code. Also the SO text editing syntax doesn't seem to handle code inside lists very well. "extractedText... " should be on its own line.

Related

How to use an array in a text-to-speech script

Hello stackoverflow Community,
I am working on a Survey which uses Text-To-Speech components. Usually they look like this:
<img src="picture.jpg" border="1" input onclick="responsiveVoice.speak('spoken_Text_here', 'Deutsch Female');" class="button1"> Text_behind_Button?
Now, I have a Database with all the Elements which will be "spoken" (or: to be used in the TTS script) and insert them in my survey software with
$key = 'A1';
$tts = dbGet($key);
So far so good. Now I would like to take the first element of the array $tts und put it in the TTS script.
<img src="picture.jpg" border="1" input onclick="responsiveVoice.speak('<?php $tts[0] ?>', 'Deutsch Female');" class="button1"> Text_behind_Button?
As a result I would like, that when you click on the button, the script takes the string from the Database and inserts it the TTS code. This way above obviously doesn't work. So I guess that I have to change the array in a string which can be read. Unfortunatly, I dont have the knoledge how to do that.
Ideas, hints and criticism (more or less) are welcome!
Best wishes

Beginner Javascript credit hour calculator

I am trying to create something in javascript that will allow me to calculate total number of credit hours. I am very new at this and I don't even know where to begin.
Here is what I need to do:
Create a web page that will allow to calculate totals number of credit hours taken by a student in the semester.
A page should include separate input fields to enter a number of one, two, and three credit hour courses, taken by a student.
Then once a result button is clicked, the total number of hours should be displayed. Use function to calculate total number of hours.
Here is what I have and I have no idea if it is even right so don't get mad:
<script>
function doSmth(){
var value = document.getElementById("credithour").value;
var doubled = value * 2;
document.getElementById("output").innerHTML = doubled;
}
</script>
<body>
1 credit hour: <input id="credit"><br />
2 credit hour: <input id="credit2"><br />
3 credit hour: <input id="credit3">
<button onclick="doSmth()">calculate</button>
<div id="output"></div>
You are pretty close but it looks like there are a few things you aren't quite grasping completely.
In your HTML you have 3 input boxes declared which is perfect and
you give them the id "credit", "credit2" and "credit3". However you
are then only grabbing one of them in your JavaScript function and
even then you are using the incorrect name "credithour"!
Then in your function you are doubling the value you have retrieved
from the input box when you are meant to be summing them all together!
Finally you are displaying it and your code for that actually looks
ok. There are some security issues around setting HTML using
innerHTML but if this is just a page for yourself and you aren't
allowing users to set strings using it you should be fine. Make a note to look into it though.
Although Stack Overflow isn't a code factory I quite like your question, it is clear and shows the effort you have done so far, so I've went to the extra effort of giving you a worked example you can hopefully use and learn from.
It isn't the most perfect JavaScript in the world but it will give you enough to get started.
I also highly recommend jsFiddle for this sort of thing, it allows you to quickly write, test and share JavaScript code with the rest of the world. Click the link below and you'll be able to edit my code, hit RUN and see how your changes affect it. You can also then SAVE your results into your own copy, just hit SAVE and copy the link!
Here is the jsFiddle that does what you are looking for: https://jsfiddle.net/jcos29eb/2/
And here is the revised code:
HTML:
<body>
1 credit hour:
<input type="number" id="credit1">
<br /> 2 credit hour:
<input type="number" id="credit2">
<br /> 3 credit hour:
<input type="number" id="credit3">
<br /> Total:
<input id="output">
<br />
<button onclick="doCalc()">Calculate</button>
</body>
JavaScript:
function doCalc()
{
var value1 = document.getElementById("credit1").value;
var value2 = document.getElementById("credit2").value*2;
var value3 = document.getElementById("credit3").value*3;
var total = Number(value1) + Number(value2) + Number(value3);
document.getElementById("output").value = total;
}
My final recommendation is to spend some time going through the W3Schools JavaScript Tutorial it is really clear, easy to understand and covers lots of JS basics and some more complicated things.

Change title of web page according to form elements

I'm looking to change the title of an html page according to certain form elements, as well as some text found on that page. I found a good site describing how using Javascript can do almost what I need, located here: http://www.devx.com/tips/Tip/13469.
The problem with the script found there is that the option to change the title is restricted to the textarea, or if I try to include another element, I get an error message. I authored web page/form templates, nothing complicated, where the intended users, who, shall we say, are not very computer literate(one of them has never used computers), fill out certain textareas and drop-down options and then save the pages in an ARCHIVE folder. To make it easier for them, I would like to give them the luxury of saving the pages without having to type the relevant date and # (each form is basically one of a series of 59), essentially standardizing the titles of the saved pages which should make it easier to categorize them using another script in the future. Can the code below(the one found in the above web site) be extended to include more than one html item, such as the select drop-down options found below, and maybe something found inside elements such as a or div?
<HTML>
<HEAD><TITLE>Change Title Dynamically:</TITLE></HEAD>
<BODY>
<FORM action="" method=POST name="SampleForm">
<B>Enter Title for the window:</B>
<TEXTAREA NAME=WindowTitle ROWS=1 COLS=50></TEXTAREA>
<INPUT TYPE=BUTTON VALUE="Change Title" ONCLICK="javascript:UpdateTitle()">
</FORM>
<SCRIPT LANGUAGE="JAVASCRIPT">
<!--
function UpdateTitle()
{
document.title = document.SampleForm.WindowTitle.value;
}
</SCRIPT>
</BODY>
</HTML>
<SELECT>
<option>-----</option>
<OPTION>JAN</OPTION>
<OPTION>FEB</OPTION>
<OPTION>MAR</OPTION>
<OPTION>APR</OPTION>
<OPTION>MAY</OPTION>
<OPTION>JUN</OPTION>
<OPTION>JUL</OPTION>
<OPTION>AUG</OPTION>
<OPTION>SEP</OPTION>
<OPTION>OCT</OPTION>
<OPTION>NOV</OPTION>
<OPTION>DEC</OPTION>
</SELECT>
I would recommend jQuery to get the values of the fields you want to display in the title and set it. More info on jQuery can be found at http://jquery.com/
You can use a jQuery selectors to get the values of the fields and concatenate it accordingly. Something like this:
document.title = $('textarea').val() + ' - ' + $('select').val();

to retain values in form using struts tags and dynamic behaviour through Javascript

I have a JSP page containing a ADD button to add the rows(a HTML code) via Javascript.
I now need to retain the value in my form by replacing the codes in JSP by struts-tags.
How should I then communicate from struts-tags and JS. Since all the HTML code lies in JS, how should it use struts-tags???
Please help!!
Your question is too vague to give an appropriate answer. However, I recently did something similar to this so I will try and give you a few guidelines.
1.) If you are hoping to populate these rows with information from the server this will require an ajax call. Most likely to an action that returns a snippet of jsp containing only a table row.
I suggest avoiding the struts2-jquery plugin for this unless you already are using it in your application. I would just use jQuery - http://api.jquery.com/jQuery.ajax/
2.) If you wish to gather user input in these rows you will just have to make sure to use appropriate naming for your fields.
Eg: In your java action you have a List< String > names.
You would need to generate the following html via js.
<tbody>
<tr>
<input type="text" name="names[0]">
</tr><tr>
<input type="text" name="names[1]">
</tr><tr>
<input type="text" name="names[2]">
</tr>
</tbody>
3.) If you wish to keep track of the number of added rows you could use this in your jsp...
<s:hidden name="rowsCount" id="rowsCount" value="0">
then within your javascript change the value of that input.
Hope one of those 3 helped!

Regex script to write data from a table to a spreadsheet

I am trying to write a script which will turn a series of basic html tables describing particular variations of certain words in different countries into a working spreadsheet for use in a database. Each table applies to the translations of a single word across countries. In html it takes the format of:
<h5><a name="akas"> equivalent names in different countries </a> </h5>
<table border="0" cellpadding="2">
<tr>
<td>character string </td>
<td> country name / country name / country name</td>
</tr>
<tr>
<td>character string </td>
<td>country name</td>
</tr>
.................. this format continues until the table ends
</table>
Country names are repeating across tables and should represent column headings on the spreadsheet across which the rows of equivalent words lie. I am totally new to regex (which I'm finding really bewildering to get into) and a beginner in Javascript also. Again I am looking for help on how to rearrange this type of data into a working spreadsheet for use in a larger database. If anyone could help me it would be really appreciated.
You should look at DOM parsing and XPath. XPath allows you to query the html file to get the content of whichever node that you need.
You can copy paste an HTML table into a spreadsheet.

Categories

Resources