How to web scrape HTML Table using PHP - php

I am trying to scrape the table from the following link and place into an array.
https://www.tradingview.com/markets/currencies/cross-rates-overview-prices/
I have tried various ways, just cannot get it right.
<?php
$htmlContent = file_get_contents("https://www.tradingview.com/markets/currencies/cross-rates-overview-prices/");
$DOM = new DOMDocument();
$DOM->loadHTML($htmlContent);
$Header = $DOM->getElementsByTagName('th');
$Detail = $DOM->getElementsByTagName('td');
//#Get header name of the table
foreach($Header as $NodeHeader)
{
$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
}
//print_r($aDataTableHeaderHTML); die();
//#Get row data/detail table without header name as key
$i = 0;
$j = 0;
foreach($Detail as $sNodeDetail)
{
$aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
$i = $i + 1;
$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;
}
//print_r($aDataTableDetailHTML); die();
//#Get row data/detail table with header name as key and outer array index as row number
for($i = 0; $i < count($aDataTableDetailHTML); $i++)
{
for($j = 0; $j < count($aDataTableHeaderHTML); $j++)
{
$aTempData[$i][$aDataTableHeaderHTML[$j]] = $aDataTableDetailHTML[$i][$j];
}
}
$aDataTableDetailHTML = $aTempData; unset($aTempData);
print_r($aDataTableDetailHTML); die();
This is the error output: (Note, there are quite a few lines of these errors)
Warning: DOMDocument::loadHTML(): Tag svg invalid in Entity, line: 405 in C:\xampp\htdocs\Testing\scraper.php on line 6
Warning: DOMDocument::loadHTML(): Tag path invalid in Entity, line: 405 in C:\xampp\htdocs\Testing\scraper.php on line 6
Warning: Undefined variable $aDataTableDetailHTML in C:\xampp\htdocs\Testing\scraper.php on line 30
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, null given in C:\xampp\htdocs\Testing\scraper.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Testing\scraper.php on line 30
Your help would be much appreciated.

There are two problems:
The errors are from validating the html. Take a look this answer how to deal with that.
The actual html where you are interested in (the table-data) are not present in the source-html. They are created via JavaScript somehow. To deal with that kind of pages you could use i.e. Selenium. See this answer on how to do that in php.

Related

Laravel store with number loop

How can I have same data stored as much as provided number?
Example
Store abc, 10 times
both abc and 10 are coming from form request
Code
nonuiqueAmount: 10
nonuiqueSerial: "abc"
if(!empty($request->input('nonuiqueSerial'))) {
foreach($request->input('nonuiqueAmount') as $item) { // this returns error
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
Error:
Invalid argument supplied for foreach()
You should use a for loop:
// nonuiqueAmount: 10
// nonuiqueSerial: "abc"
if (!empty($request->input('nonuiqueSerial'))) {
for ($i = 0; $i < $request->input('nonuiqueAmount', 0); ++$i) { // I've added the zero as a default value to prevent unnecessary loops
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}
}
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. w3schools docs
Your nonuiqueAmount is an int. I would suggest simply stick with basic for loop
for ($x = 0; $x < $request->input('nonuiqueAmount'); $x++) {
$barcode = new Barcode;
$barcode->serial_number = $request->input('nonuiqueSerial');
$barcode->save();
}

XML attributes function error say Called on a non-object

I have XML file I want to retrieve the attribute names from it by using this code below. the code work and retrieve all attribute names but under the last attribute names also show this error
Fatal error: Call to a member function attributes() on a non-object in C:\wamp\www\php\xml\EnglishArabic\new.php on line 13
<?php
$en = simplexml_load_file('ENstrings.xml');
$enlen = sizeof($en->string);
for ($i=0; $i<=$enlen ; $i++) {
foreach ($en->string[$i]->attributes() as $key => $value) {
echo $value. '<br \>';
}
}
?>
That's because the index ($en->string[$i]) starts from 0 to length-1, so your loop supposed to stop just before $i reaches the length :
for ($i=0; $i<$enlen ; $i++) {

Warning Illegal string offset in PHP [duplicate]

This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 7 years ago.
I have this chunk of PHP code which is giving me the error:
Warning: Illegal string offset 'mod_modulecode' in C:\xampp\htdocs\MP\multi\functions.php on line 29
This is the code that the warning is relating to:
function set_rights($menus, $menuRights) {
$data = array();
for ($i = 0, $c = count($menus); $i < $c; $i++) {
$row = array();
for ($j = 0, $c2 = count($menuRights); $j < $c2; $j++) {
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
if (authorize($menuRights[$j]["rr_create"]) || authorize($menuRights[$j]["rr_edit"]) ||
authorize($menuRights[$j]["rr_delete"]) || authorize($menuRights[$j]["rr_view"])
) {...................'
Any help would be greatly appreciated.
What PHP is saying is that at some iteration in
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
$menus[$i] is a string, not an array, and you're trying to access it like an array containing the key mod_modulecode.
To help track down this bug, I'd suggest:
if(is_string($menus[$i]) {
var_dump('string bug','i',$i,'j',$j,'$menus[$i]',$menus[$i]);
}
if ($menuRights[$j]["rr_modulecode"] == $menus[$i]["mod_modulecode"]) {
Or even better yet, if you have xdebug installed, which will show local variables when an uncaught exception is thrown: throw new Exception('string bug');

prevent output of data if array not found

I have this piece of code which works well if the ['extensions'] array exists.. but if the array does not exist then it returns errors. How can I fix this code to not return anything if the extensions array does not exist?
-[UPDATE-
Sorry i had previously inserted the wrong code.. here is the proper code i need checked.
$oid = array('id-ce-subjectAltName');
$count = count($cert['tbsCertificate']);
for($i = 0; $i < $count; $i++) {
if(array_key_exists('extensions', $cert['tbsCertificate']) &&
in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n",'<b>[SANs]</b>',"\n","\n";
}
}
I get this warning when ['extensions'] does not exist - I would like to prevent any warnings from being generated.
Notice: Undefined index: extensions in
C:\xampp\htdocs\labs\certdecode\certdecode.php on line 142
AFTER UPDATE:
How is the structure of the array?
You count the number of items in $cert['tbsCertificate'],
but in your loop, your $i is for the number of items in $cert['tbsCertificate']['extensions'].
So maybe you try to do something like this?:
$oid = array('id-ce-subjectAltName');
if (array_key_exists('extensions', $cert['tbsCertificate']) &&
is_array($cert['tbsCertificate']['extensions'])
) {
$count = count($cert['tbsCertificate']['extensions']);
for ($i = 0; $i < $count; $i++) {
if (in_array($cert['tbsCertificate']['extensions'][$i]['extnId'], $oid)) {
$value = $cert['tbsCertificate']['extensions'][$i]['extnId'];
echo "\n", '<b>[SANs]</b>', "\n", "\n";
}
}
}
okay, seems to work by adding an isset() at the start of the code:
if(isset($cert['tbsCertificate']['extensions'])) {
thanks guys!

Warning: Illegal offset type in

I have some problem with this code. Warning: Illegal offset type in Line 22
$this->word[$kata][]=array($i,$j);
and the full code is below
private $jmldoc = 0; private $word = array();
public function getIndex($D) {
$this->jmldoc = count($D);
for($i=0; $i<$this->jmldoc; $i++) {
$pp = new prePro($D[$i]);
$kata = $pp->tokenize();
$n = count($kata);
for($j=0; $j<$n; $j++) {
$this->word[$kata]=array($i,$j);
}
}
}
Can you help me to fix it?
You are passing an array, not a string/integer index to your $this->word.
//I suppose from the context of your code that $kata is an array also
//so if that's true, it can't be used as an index
$this->word[$kata][]=array($i,$j);
Keep in mind that $this->word is an array. So probably there is something wrong with your program logic. To fix this, use an integer or string to access the elements of an array.

Categories