Reputation: 49
How can I skip "Attempt to read property "plaintext" sometimes the object doesn't exist,
see screen shot:
I need a way to skip the find option if the object isn't present.
this is my code as of now:
<?php
include 'simple_html_dom.php';
include '../lib/_config.php';
// URL to scrape
$url = "https://m.spareroom.com/rooms-for-rent/ca/sacramento_county/rancho_cordova/102552139";
//$url = "https://m.spareroom.com/rooms-for-rent/ca/sacramento_county/rancho_cordova/102553449";
// Get the HTML content
$html = file_get_html($url);
foreach($html->find('h1.heading') as $title)
echo $title->innertext . '<br>';
################################################################################
foreach($html->find('span.key-features__price') as $price)
$price->innertext;
$clean_price = clean_price($price);
echo $clean_price . '<br>';
foreach($html->find('li.key-features__feature') as $location)
echo $location->plaintext. '<br>';
echo "<h3> Description </h3>";
foreach($html->find('div.feature__description-body') as $description)
echo $description->plaintext . "<br>";
####### Availability ###########################################################
echo "<h3> Availability </h3>";
$available = $html->find('dd.feature-list__value', 0)->plaintext;
echo "Available : " . $available . "<br>";
$min_term = $html->find('dd.feature-list__value', 1)->plaintext;
echo "Minimum term : " . $min_term . "<br>";
$max_term = $html->find('dd.feature-list__value', 2)->plaintext;
echo "Maximum term : " . $max_term . "<br>";
###### Extra cost ##############################################################
echo "<h3> Extra cost </h3>";
$deposit = $html->find('dd.feature-list__value', 3)->plaintext;
echo "Deposit : " . $deposit . "<br>";
$utilities = $html->find('dd.feature-list__value', 4)->plaintext;
echo "Utilities included? : " . $utilities . "<br>";
$fees_apply = $html->find('dd.feature-list__value', 5)->plaintext;
echo "Fees apply? : " . $fees_apply . "<br>";
#### Amenities #################################################################
echo "<h3> Amenities </h3>";
$furnishings = $html->find('dd.feature-list__value', 6)->plaintext;
echo "Furnishings : " . $furnishings . "<br>";
$parking = $html->find('dd.feature-list__value', 7)->plaintext;
echo "Parking : " . $parking . "<br>";
$garage = $html->find('dd.feature-list__value', 8)->plaintext;
echo "Garage : " . $garage . '<br>';
$garden_terrace = $html->find('dd.feature-list__value', 9)->plaintext;
echo "Garden/terrace : " . $garden_terrace . '<br>';
$balcony_patio = $html->find('dd.feature-list__value', 10)->plaintext;
echo "Balcony/patio : " . $balcony_patio . "<br>";
$disabled_access = $html->find('dd.feature-list__value', 11)->plaintext;
echo "Disabled access : " . $disabled_access . "<br>";
$living_room = $html->find('dd.feature-list__value', 12)->plaintext;
echo "Living room : " . $living_room . "<br>";
$broadband = $html->find('dd.feature-list__value', 13)->plaintext;
echo "Broadband : " . $broadband . "<br>";
################################################################################
#### Current household #########################################################
echo "<h3> Current household </h3>";
if ($html->find('dd.feature-list__value', 14)){
$number_of_roommates = $html->find('dd.feature-list__value', 14)->plaintext;
echo "Number Of Roommates : " . $number_of_roommates . "<br>";
}
else
{
}
if ($html->find('dd.feature-list__value', 15)){
$total_rooms = $html->find('dd.feature-list__value', 15)->plaintext;
echo "Total # rooms : " . $total_rooms . "<br>";
}
else
{
echo "Total # rooms : " . $total_rooms . "<br>";
}
$ages = $html->find('dd.feature-list__value', 16)->plaintext;
echo "Ages : " . $ages . "<br>";
$smoking_ok = $html->find('dd.feature-list__value', 17)->plaintext;
echo "Smoking Ok : " . $smoking_ok . "<br>";
$pets_ok = $html->find('dd.feature-list__value', 18)->plaintext;
echo "Pets Ok : " . $pets_ok . "<br>";
$langauage = $html->find('dd.feature-list__value', 19)->plaintext;
echo "Language : " . $langauage . "<br>";
$nationality = $html->find('dd.feature-list__value', 20)->plaintext;
echo "Nationality : " . $nationality . "<br>";
$roommate_occupation = $html->find('dd.feature-list__value', 21)->plaintext;
echo "Roommate Occupation : " . $roommate_occupation . "<br>";
$interest = $html->find('dd.feature-list__value', 22)->plaintext;
echo "Interests : " . $interest . "<br>";
$house_gender = $html->find('dd.feature-list__value', 23)->plaintext;
echo "Gender : " . $house_gender . "<br>";
#### New roommate preferences ##################################################
echo "<h3> New roommate preferences </h3>";
$couples_ok = $html->find('dd.feature-list__value', 24)->plaintext;
echo "Couples ok? " . $couples_ok . "<br>";
$smoking_ok = $html->find('dd.feature-list__value', 25)->plaintext;
echo "Smoking ok? " . $smoking_ok . "<br>";
if ($html->find('dd.feature-list__value', 26)){
$pets_ok = $html->find('dd.feature-list__value', 26)->plaintext;
echo "Pets ok? " . $pets_ok . "<br>";
}
if ($html->find('dd.feature-list__value', 27)){
$roommate_occupation = $html->find('dd.feature-list__value', 27)->plaintext;
echo "Occupation? " . $roommate_occupation . "<br>";
}
if ($html->find('dd.feature-list__value', 28)){
$room_references = $html->find('dd.feature-list__value', 28)->plaintext;
echo "References? " . $room_references . "<br>";
}
if ($html->find('dd.feature-list__value', 29)){
$min_age = $html->find('dd.feature-list__value', 29)->plaintext;
echo "Min age " . $min_age . "<br>";
}
if ($html->find('dd.feature-list__value', 30)){
$max_age = $html->find('dd.feature-list__value', 30)->plaintext;
echo "Max age " . $max_age . "<br>";
}
if ($html->find('dd.feature-list__value', 31)){
$gender_preferred = $html->find('dd.feature-list__value', 31)->plaintext;
echo "Gender Prefered " . $gender_preferred . "<br>";
}
#### User Profile Name ##################################################
echo "<h3> User Profile Name </h3>";
foreach($html->find('strong.profile-photo__name') as $user_profile)
echo $user_profile->plaintext . '<br>';
foreach($html->find('ul.thumbs img') as $image_one)
echo $image_one->src . '<br>';
################################################################################
?>
"How can I modify my PHP script to skip attempting to read the 'plaintext' property if the object doesn't exist, thereby avoiding errors when scraping website data with simple_html_dom.php?"
Upvotes: 0
Views: 60
Reputation: 1
echo (isset($description->plaintext)) ? $description->plaintext : "" . "
";
Upvotes: 0