Python webscraper only returns 'None' - python

I am trying to write a program to scrape eBay to show me an inventory of products, but all I get printed to the terminal is 'None'. I am expecting a title of the product.
I've changed my code to 'find' different keywords, but the output is always the same 'None', usually that means that it can't find the specified keyword, but the keywords I enter are shown on the web page and in 'inspect element'.
Here's a snippet:
def get_detail_data(soup):
try:
title = soup.find('div', class_="it-ttl").find('itemprop')
except:
title = ''
I am trying to fetch only the results that correspond with the keyword I enter into the program.

Probably the selector is wrong. I was able to extract titles from eBay HTML with this code.
from bs4 import BeautifulSoup
import requests
import os
params = {"_nkw": "coffee"}
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 OPR/66.0.3515.103"
}
proxies = {"http": os.getenv("PROXY_URL")}
# r = requests.get("https://www.ebay.com/sch/i.html", params=params, headers=headers, proxies=proxies)
# html = r.content
html = open("ebay.html", "r").read()
soup = BeautifulSoup(html, "lxml")
titles = [title.get_text() for title in soup.select("h3.s-item__title")]
print(titles)
Output
['Custom Photo Mug Coffee Cup Picture Photo Text Phrase Logo Personalized Gift', 'Automatic Self Stirring Coffee Mixing Cup Mug 400ml Electric Stainless Steel NEW', 'Grinch Cup Of Fuckoffee One Splash No One Cares Gift Black Coffee Mug', '6 Pack Stainless Steel Coffee Soup Mug Tumbler Camping Mug Cup 12oz ', 'BULLET THERMOS 16oz Coffee Hunting Hot Insulated Stainless Steel Cup Mug Flask', 'Lot of 1-48 Pack Stainless Steel Coffee Soup Mug Tumbler Camping Mug Cup 16 oz ', 'Custom Magic Coffee Cup Personalized Image Photo Text Color Changing Mug Gift', '400ml Automatic Self Stirring Mug Coffee Cup Mixer Tea Home Insulated Green', 'Set of 2 Strong Glass Double Wall Coffee Mug Tea Espresso Cup 16oz with Wood Lid', 'Funny Mug Donald Trump 45 Coffee Mug President Gun Right Cup Political Gifts Mug', 'Custom Photo Mug Coffee Cup Picture Photo Text Phrase Logo Personalized Gift 15', 'Trump 2020 Keep America Great Office Work Cup Gift Coffee Tea Ceramic Mug', 'New ListingNew Custom Red coffee cup set', 'Green Self Stirring Mug Coffee Cup Tea Auto Mixer Drink Insulate Stainless 400ml', 'Have A Nice Day Coffee Mug Middle Finger Funny Cup for Milk, Juice, Tea, Coffee ', 'Custom Magic Coffee Cup Personalized Image Photo Text Heart Handle Inner Colour', 'New ListingJoe Biden & Harris 46th President Inauguration Day 2021 Coffee Mug Gifts Cup', 'Travel Coffee Mug Stainless Steel Lid Tea Drink Tea Cup Handle Double Wall 10 Oz', 'MAGA Make America Great Again 45th President Donald Trump Coffee Mug Cup', 'Black Pistol Gun Mug Coffee Tea Beverage Cup Novelty Handgun Grip Joke Gift', 'Auto Mixing coffee cup Stainless Electric Lazy Self Stirring Mug Tea Mug', 'Personalized Coffee Mug Custom Photo Text Logo Name Printed Gift Ceramic Cup ', 'Baby Yoda The Child Mandalorian Mug No Coffee No Forcee Meme Ceramic ( Black)', 'HOT & Cold Double Insulated Self Stirring Funny Joke Mug Electric Coffee Cup WS', '400ML Camera Lens Cup Coffee Travel Mug Thermos Stainless Steel Leak-Proof Lid', 'Trump 2020 Keep America Great - Ceramic Coffee Mug Tea Cup', 'Mug Set of 5, Coffee Cup with Accent Spoon Tea Cups 12 Oz. Porcelain Mugs Black', 'MAGA Make America Great Again 45th President Donald Trump Coffee Mug Cup', 'Starbucks Style 18oz Stainless Steel Thermal Insulated Chubby Coffee Mug Cup', 'To My Wife Gift Mug - Never Forget That I Love You - Coffee Mug Tea Cup Gift', 'Donald Trump Twitter Ban Funny Gift White Coffee Mug 11Oz 15Oz', "New ListingFunny Valentine's Coffee Mug for Men - Thanks for All the Coffee Mug - Valentine", 'Bruntmor Porcelain Coffee Cups Mugs Set of 6 Large sized 12 Ounce Matte Black', 'New ListingRare! MAC MILLER Coffee Mug Cup Tea 11oz ', 'New ListingGifts For Husband - You Are My Everything Coffee Mug - Meaningful Gift Idea ', 'New ListingCeramic Coffee Tea Cup Gift, Funny Coffe Mug Gift Mug Coffee Tea Cup ', 'I love catturd mug Perfect gift for valentine day, Best gift ever', 'New ListingWorlds Best Boss Mug 12oz Manager Gift Supervisor Coworker Coffee Cup', 'Maxam 15oz Double Wall Stainless Steel Coffee Cup', 'To My Gorgeous Wife Meeting You Was Fate Black Ceramic 11oz 15oz Coffee Tea Cup', 'New ListingFunny Naruto Coffee Tea Cup Mug', 'New ListingHallmark Signature Sunflower Coffee Cup Mug Always', 'New ListingInspired ! Kiss Band Ricks White Tea Coffee Mug Cup 11oz', '12V Electric Heated Travel Mug Stainless Steel Coffee Tea Cup Warmer Heater NEW', 'Stainless Steel Insulated Double Wall Travel Coffee Mug Cup 14 Oz Thermos Tea !!', 'New ListingJoe Biden -Trump Coffee Mug Cup 11oz Tea Coffee', 'New ListingA Ceramic Coffee Mug Beverage Cup ..."Shopping Solves Everything" ', 'The Big Guy Coffee Mug Joe Biden Sunglasses Red White Blue Election Coffee Cup', 'Bruntmor Ceramic Stacking Coffee Mug Set Tea Cup Set of 6 Matte Black 18 Ounce', 'New ListingThankful Porcelain Ceramic Thanksgiving and Fall Coffee Mug', 'To My Husband Never Forget That I Love You If Could Give One Gift Coffee Mug', 'Stainless Steel Insulated Travel Coffee Mug CUP 16 OZ NEW! PURPLE color', 'Funny Parody Middle Finger Coffee Mug Home Office Ceramic Porcelain Tea Cup Gift', '2pk Donut Warming White Ceramic Coffee Mug Set 20oz Cup Drip Trap Mustache Guard', 'Firefly Serenity Shiny Coffee Mug Tea Cup Novelty Gift Mugs', 'Polka Dot Coffee Cups Mugs Set of 6 Large sized 16 Ounce Ceramic Coffee Mug Set', 'Customized Prescription Bottle Coffee Mug Tea Cup', 'Camera Lens 24-105mm Travel Coffee Mug / Cup with Drinking Lid Best Gift', "New ListingCeramic Coffee Mug with Joe Biden's Big Cup Campaign Design for Presidents 11oz", 'Stainless Steel Double Wall Mug Cup Portable Travel Coffee Tea Cups 500/350ml', 'New ListingCalm The Hell Down Coffee Mug', 'Ceramic Coffee Cups Mugs Set of 6 Large-sized 16 Ounce Grooved Mugs Matte Black', "Big Sister Mug - Big Sister Gift - God Created My World's Best Funny Coffee Cup"]

Related

Why is Selnium not able to find element by class?

I was trying to extract some data from the website but the code below just returns a empty list when there is clearly an element by the class name given in the code.
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://terraria.fandom.com/wiki/Weapons")
search = driver.find_elements(By.CLASS_NAME, "infocard clearfix terraria compact")
print(search)
I am really puzzled, I even tried using sleep to give the website time to load but it didn't affect the result. Any help will be much appreciated.
You are getting an empty data item because infocard clearfix terraria compact did't select any data point. The following element selector/locator strategy is working.
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
#chrome to stay open to see what's happening in the real word or make it comment to close
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
URL ='https://terraria.fandom.com/wiki/Weapons'
driver.get(URL)
time.sleep(5)
for item in driver.find_elements(By.XPATH,'//*[#class="itemlist"]/ul/li/span/span/span/a'):
print(item.text)
Output:
Copper Shortsword
Tin Shortsword
Wooden Sword
Boreal Wood Sword
Copper Broadsword
Iron Shortsword
Palm Wood Sword
Rich Mahogany Sword
Cactus Sword
Lead Shortsword
Silver Shortsword
Tin Broadsword
Ebonwood Sword
Iron Broadsword
Shadewood Sword
Tungsten Shortsword
Gold Shortsword
Lead Broadsword
Silver Broadsword
Bladed Glove
Tungsten Broadsword
Zombie Arm
Gold Broadsword
Platinum Shortsword
Mandible Blade
Stylish Scissors
Ruler
Platinum Broadsword
Umbrella
Breathing Reed
Gladius
Bone Sword
Bat Bat
Tentacle Spike
Candy Cane Sword
Katana
Ice Blade
Light's Bane
Tragic Umbrella
Muramasa
Exotic Scimitar
Phaseblade
Blood Butcherer
Starfury
Enchanted Sword
Purple Clubberfish
Bee Keeper
Falcon Blade
Blade of Grass
Fiery Greatsword
Night's Edge
Pearlwood Sword
Classy Cane
Slap Hand
Cobalt Sword
Palladium Sword
Phasesaber
Ice Sickle
Brand of the Inferno
Mythril Sword
Orichalcum Sword
Breaker Blade
Cutlass
Frostbrand
Adamantite Sword
Beam Sword
Titanium Sword
Fetid Baghnakhs
Bladetongue
Tizona
Ham Bat
Excalibur
True Excalibur
Chlorophyte Saber
Death Sickle
Psycho Knife
Keybrand
Chlorophyte Claymore
The Horseman's Blade
Christmas Tree Sword
True Night's Edge
Seedler
Flying Dragon
Starlight
Terra Blade
Influx Waver
Star Wrath
Meowmere
Wooden Yoyo
Rally
Malaise
Artery
Amazon
Code 1
Valor
Cascade
Format:C
Gradient
Chik
Hel-Fire
Amarok
Code 2
Yelets
Red's Throw
Valkyrie Yoyo
Kraken
The Eye of Cthulhu
Terrarian
Spear
Trident
Storm Spear
The Rotted Fork
Swordfish
Dark Lance
Cobalt Naginata
Palladium Pike
Mythril Halberd
Orichalcum Halberd
Adamantite Glaive
Titanium Trident
Gungnir
Ghastly Glaive
Chlorophyte Partisan
Tonbogiri
Mushroom Spear
Obsidian Swordfish
North Pole
Wooden Boomerang
Enchanted Boomerang
Fruitcake Chakram
Bloody Machete
Shroomerang
Ice Boomerang
Thorn Chakram
Combat Wrench
Flamarang
Flying Knife
Sergeant United Shield
Light Disc
Bananarang
Possessed Hatchet
Paladin's Hammer
Chain Knife
Mace
Flaming Mace
Ball O' Hurt
The Meatball
Blue Moon
Sunfury
Anchor
KO Cannon
Drippler Crippler
Chain Guillotines
Dao of Pow
Flower Pow
Golem Fist
Flairon
Terragrim
Arkhalis
Jousting Lance
Shadowflame Knife
Hallowed Jousting Lance
Sleepy Octopod
Scourge of the Corruptor
Shadow Jousting Lance
Vampire Knives
Sky Dragon's Fury
Daybreak
Solar Eruption
Zenith
Wooden Bow
Boreal Wood Bow
Copper Bow
Palm Wood Bow
Rich Mahogany Bow
Tin Bow
Ebonwood Bow
Iron Bow
Shadewood Bow
Lead Bow
Silver Bow
Tungsten Bow
Gold Bow
Platinum Bow
Demon Bow
Tendon Bow
Blood Rain Bow
The Bee's Knees
Hellwing Bow
Molten Fury
Sharanga
Pearlwood Bow
Marrow
Ice Bow
Daedalus Stormbow
Shadowflame Bow
Phantom Phoenix
Pulse Bow
Aerial Bane
Tsunami
Eventide
Phantasm
Cobalt Repeater
Palladium Repeater
Mythril Repeater
Orichalcum Repeater
Adamantite Repeater
Titanium Repeater
Hallowed Repeater
Vulcan Repeater
Chlorophyte Shotbow
Stake Launcher
Red Ryder
Flintlock Pistol
Musket
The Undertaker
Sandgun
Revolver
Minishark
Boomstick
Quad-Barrel Shotgun
Handgun
Phoenix Blaster
Pew-matic Horn
Clockwork Assault Rifle
Gatligator
Shotgun
Onyx Blaster
Coin Gun
Uzi
Megashark
Venus Magnum
Tactical Shotgun
Sniper Rifle
Candy Corn Rifle
Chain Gun
Xenopopper
Vortex Beater
S.D.M.G.
Grenade Launcher
Proximity Mine Launcher
Rocket Launcher
Nail Gun
Stynger
Jack 'O Lantern Launcher
Snowman Cannon
Celebration
Electrosphere Launcher
Celebration Mk2
Paper Airplane
White Paper Airplane
Shuriken
Throwing Knife
Poisoned Knife
Snowball
Spiky Ball
Bone
Rotten Egg
Star Anise
Molotov Cocktail
Frost Daggerfish
Javelin
Bone Javelin
Bone Throwing Knife
Grenade
Sticky Grenade
Bouncy Grenade
Beenade
Happy Grenade
Flare Gun
Ale Tosser
Blowpipe
Blowgun
Snowball Cannon
Paintball Gun
Harpoon
Egg Cannon
Star Cannon
Toxikarp
Dart Pistol
Dart Rifle
Flamethrower
Piranha Gun
Elf Melter
Super Star Shooter
Wand of Sparking
Thunder Zapper
Amethyst Staff
Topaz Staff
Sapphire Staff
Emerald Staff
Ruby Staff
Diamond Staff
Amber Staff
Vilethorn
Weather Pain
Magic Missile
Aqua Scepter
Flower of Fire
Flamelash
Sky Fracture
Crystal Serpent
Flower of Frost
Frost Staff
Crystal Vile Shard
Life Drain
Meteor Staff
Poison Staff
Rainbow Rod
Unholy Trident
Tome of Infinite Wisdom
Venom Staff
Nettle Burst
Bat Scepter
Blizzard Staff
Inferno Fork
Shadowbeam Staff
Spectre Staff
Resonance Scepter
Razorpine
Staff of Earth
Betsy's Wrath
Gray Zapinator
Space Gun
Bee Gun
Roman candle
Laser Rifle
Orange Zapinator
Zapinator
Wasp Gun
Leaf Blower
Rainbow Gun
Heat Ray
Laser Machinegun
Charged Blaster Cannon
Bubble Gun
Water Bolt
Book of Skulls
Demon Scythe
Cursed Flames
Golden Shower
Crystal Storm
Magnet Sphere
Razorblade Typhoon
Lunar Flare
Crimson Rod
Ice Rod
Clinger Staff
Nimbus Rod
Magic Dagger
Medusa Head
Spirit Flame
Shadowflame Hex Doll
Blood Thorn
Magical Harp
Toxic Flask
Nightglow
Stellar Tune
Nebula Arcanum
Nebula Blaze
Last Prism
Finch Staff
Slime Staff
Flinx Staff
Abigail's Flower
Hornet Staff
Vampire Frog Staff
Imp Staff
Blade Staff
Spider Staff
Pirate Staff
Sanguine Staff
Optic Staff
Deadly Sphere Staff
Pygmy Staff
Raven Staff
Desert Tiger Staff
Tempest Staff
Terraprisma
Xeno Staff
Stardust Cell Staff
Stardust Dragon Staff
Lightning Aura Rod
Flameburst Rod
Explosive Trap Rod
Ballista Rod
Houndius Shootius
Queen Spider Staff
Lightning Aura Cane
Flameburst Cane
Explosive Trap Cane
Ballista Cane
Staff of the Frost Hydra
Lightning Aura Staff
Flameburst Staff
Explosive Trap Staff
Ballista Staff
Lunar Portal Staff
Rainbow Crystal Staff
Leather Whip
Snapthorn
Spinal Tap
Firecracker
Cool Whip
Durendal
Dark Harvest
Morning Star
Kaleidoscope
Snowball Launcher
Cannon
Bunny Cannon
Land Mine
Bomb
Sticky Bomb
Bouncy Bomb
Scarab Bomb
Dynamite
Sticky Dynamite
Bouncy Dynamite
Bomb Fish
Holy Hand Grenade
Holy Water
Unholy Water
Blood Water
Boring Bow
Etherian Javelin
First Fractal
Skull Bow
Scythe
Soul Scythe
Icemourne
WebdriverManager

How to print results from Amazon after searching up a phrase using Python, Selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.amazon.com.au/?&tag=hydramzkw0au-22&ref=pd_sl_6wcb7ezr9q_e&adgrpid=87712394315&hvpone=&hvptwo=&hvadid=414047633191&hvpos=&hvnetw=g&hvrand=17758126363033746916&hvqmt=e&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1000286&hvtargid=kwd-10573980&hydadcr=179_487&gclid=CjwKCAiA8bqOBhANEiwA-sIlNxQufQ7Vywkurf9_zmHedwoBR-vCcDaw7zLcvdHmtp23d7OxJeEgThoCxu8QAvD_BwE")
link = driver.find_element(By.ID, "twotabsearchtextbox")
link.send_keys("Wooden box")
link.send_keys(Keys.RETURN)
try:
main = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "sg-col-inner"))
)
results = main.find_elements(By.CLASS_NAME, "sg-col-inner")
for result in results:
resultname = results.find_element(By.CLASS_NAME, "a-size-base-plus a-color-base a-text-normal")
print(resultname)
except:
driver.quit()
I am having problems with the for loop, I am able to search 'Wooden Box', but I am unable to print the results that I get from searching 'Wooden Box. I am very new to python and any help is appreciated.
To print the results attributes you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following Locator Strategies:
Code Block:
driver.get("https://www.amazon.com.au/?&tag=hydramzkw0au-22&ref=pd_sl_6wcb7ezr9q_e&adgrpid=87712394315&hvpone=&hvptwo=&hvadid=414047633191&hvpos=&hvnetw=g&hvrand=17758126363033746916&hvqmt=e&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1000286&hvtargid=kwd-10573980&hydadcr=179_487&gclid=CjwKCAiA8bqOBhANEiwA-sIlNxQufQ7Vywkurf9_zmHedwoBR-vCcDaw7zLcvdHmtp23d7OxJeEgThoCxu8QAvD_BwE")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#twotabsearchtextbox"))).send_keys("Wooden box" + Keys.RETURN)
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='sg-col-inner']//div[#class='a-section a-spacing-small a-spacing-top-small']/span[#dir='auto' and contains(., 'results')]"))).text)
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[#class='a-size-base-plus a-color-base a-text-normal']")))])
Console Output:
['Nirvana Class Handmade Wooden Jewellery Trinket Box Keepsake Storage Organizer with Hand Carved Celtic Design', 'Large Wooden Box with Hinged Lid - Wood Storage Box with Lid - Black Stash Box - Wooden Storage Box - Decorative Boxes with lids Stash Boxes (Matte Black)', 'Wooden Box Jewelry Trinket Box Storage Organizer Trinket Storage.', 'TRUST- Wooden Box Keepsake Jewelry Trinket Box Storage Organizer Trinket Storage Keepsake Jewelry', 'Handmade Wooden Jewelry Box/Case / Storage for Women Jewel Organizer Gift Items', '(Dark Brown) - Natural Wood Stash Box with Rolling Tray Lid w/ Extra Large Storage - Dark Brown - Premium Quality Dovetail Design Discrete Wooden Boxes Bamboo Home Decor (Dark Brown)', '3 Piece Wooden Treasure Box - Keepsake Box - Treasure Chest with Flower Motif for Jewelry', 'Unfinished Wooden Box Bundled with 6 Tubes of Paint and 3 Paintbrushes — Stash Box with Hinged Lid and Front Clasps — Decorative Box, Storage Box, Photo Box — DIY Craft — 9.8 x 7.8 x 4.7 Inches', 'Wooden Box with Hinged Lid, EXISTING Wood Storage Box with Lid, Wooden Memory Keepsake Box - Store Jewelry, Keepsakes, Trinkets and Treasure in Decorative Jewelry Box - Small Walnut Wood Box', '1PC Wooden Ring Box Wooden Printed Personalized Rustic Wedding Jewelry Ring Holder Valentines Day Anniversary Engagement Wedding Gift Jewelry Box', 'IdeaWoodCompany Ring Bearer Box - Pillow Alternative - Ring Holder Box - Wooden Wedding mrs mr Ring Box - Double Ring Box for Wedding Ceremony', 'Sumnacon Wooden Rectangular Tissue Box Cover - Multifunctional Tissue Box Holder with Stationery Remote Control Box, Decorative Tissue Pen Remote Organizer for Home/Office/Car/Restaurant (Retro Yellow)', 'ITOS365 Handmade Wooden Keepsake Storage Case Jewelry Box Jewel Organizer - Gifts for Women, 8 x 5 Inches', 'Wooden Stash Box with Rolling Tray Stash Box Combo to Organise your Herbs and Accessories - Rolling Kit with Removable divider - Large stash box and Jewelry box (Bamboo)', 'Antique Style Wood Storage Box - Decorative box for Home or Office - Wooden Box with Hinged Lid -', 'Blood Will (aka The Unwilling)', 'Melissa & Doug MND488 Band-in-a-Box Clap Clang Tap Musical Instruments (Various Instruments, Wooden Storage Crate, 10-Piece Set, 7.62 cm H x 28.702 cm W x 37.846 cm L)', 'Wood Planter Box, 5 Inch Square, Rustic Barn Wood, Plastic Liner, Garden Centerpiece Display, Wedding Flowers Holder, Home and Venue Decor, (Set of 4)', 'Blake & Lake Tree of Life Keepsake Box - Wooden Keepsake Box with Hinged Lid - Engraved Design - Decorative Wooden Boxes with Lid - Wood Gift Box (Tree of Life)', 'BEAUDORA Jewellery Music Box Wooden Pink Princess Dream Castle Storage Organizer Chest Girls Alice Melody', 'Storage Box, Wooden Storage Box 25 Compartment Essential Oil Container, Essential Oil Display Holder 24 Slots for Essential Oil Storage Box Shop Home Use DIY', 'Large Wooden Stash Box Made of Bamboo. With Sliding No Mess Rolling Tray, Brush & 3 Airtight Smell Proof Jars. Perfect Sturdy Container Kit for Storing Your Herbs and Accessories', 'Large Storage Box with Sliding Lid - Antique Box - Large Storage Box with lid and Combo Lock - Wooden Keepsake Box with Lock - Wood Gift Boxes (Box with Extra Tray)', 'Small Wooden Storage Box with Drawers, Desk Organizer, Vintage French Design (9.75 x 7 x 5 In)', 'Small Jewelry Box Decorative Wooden Jewelry Box, Trinket Box, Mini Storage Chest for Jewelry, Memento Case, Wood Holder for Miscellaneous Teabags Coins, Rustic Antique Distressed Container', 'KidKraft 14953 Austin Toy Box Natural', 'Victrola Wooden Record Crate, Wood color', 'Aroma Designs Wooden Box with Labels for Essential Oil Bottles (Holds 25)', 'Great Birthday Gifts Handmade Decorative Wooden Jewelry Box Jewelry Organizer Keepsake Box Treasure Chest Trinket Holder Watch Box Storage Lock Box 8 x 5 Inches Housewarming Gift Ideas', 'YGEOMER 2pcs 42oz Loaf Soap Mold, Rectangular Silicone Mold Set for Making Soap, with Wooden Boxes, 2 Cutters and 100pcs 4x6 inches Bags', 'ROKR 3D Wooden Puzzles Model Building Kits Mechanical Music Box Kits Creative Gift for Kids on Birthday/Christmas Day', 'Lulli Dried Herbs for Witchcraft - 24 Bottles of Magical Herbal Supplies for Pagan, Wiccan, Witch Spells & Ritual - with Lavender, Mugwort, Sage, Thistle - Essential Kit for Altar, Magic (Wooden Box)', 'Large Wooden Box with Hinged Lid - Wood Storage Box with Lid - Wooden Keepsake Box - Decorative boxes with lids (Black)', 'Double Watch Winders for Automatic Watches ,Solid Wooden 2 Watch Winder Box for Automatic Watches with Super Quiet Motor Watch Spinner ,5 Rotation Mode Settings with Blue Led Light, Soft Modern Watch Pillows ,Two Watch Winder Fit Women and Men Gifts', 'Bits and Pieces - The Secret Enigma Gift Box - Wooden Brainteaser Puzzle Box', 'DharmaObjects Hand Carved Tree of Life Wooden Box Keepsake Storage Multi Utility', 'Soul & Lane Miranda Decorative Wooden Storage Boxes (Set of 2) | Wooden Chest Trunk for Keepsake Toys Photos Memories Closet Nursery Office Bedroom Decoration', 'MOROBOR Jewelry Box Antique Lock Latch Hasp Hinges Handle Box Corner Protectors Kit for DIY Jewelry Box Bronze, Antique Brass Wood Case Jewelry Chest Storage Box Feet Leg Corner Protector, Box not Included', 'EZDC Set of 3 Nesting Wooden Crates, 16 x 12” Wall Mounted Wooden Basket, Storage Crates, Wooden Crate Box for Storage, Display, Decoration', 'Wooden Storage box with Compartments and Key Lock - Locking Keepsake Box for Collectibles - Decorative Wood Box with Hinged Lid (Dark Brown)', 'Stonebriar Vintage Worn Blue Floral Wooden Keepsake Box with Hinged Lid, Storage for Trinkets and Memorabilia, Decorative Jewelry Holder, Gift Idea for Birthdays, Christmas, Weddings, or Any Occasion', 'ROKR 3D Wooden Puzzle Orrery Music Box - Mechanical DIY Solar System Kit Musical Hands-on Activity Toys Gifts for Teens Man/Woman Family', 'WaaHome Small Treasure Chest Decorative Wooden Treasure Box', '55-606 30009 Set of 9 Screwdrivers with 9 Tubes with Spare Blades in Wooden Box Watch Repair Kit', 'Wooden Stash Box Large – Handmade Decorative Stash Box - 9” x 9” x 3.5” Storage Box - Premium Quality Dovetail Design Discrete Wood Stash Boxes (large)', 'Wooden Essential Oil Box - Holds 25 Bottles Size 5-15 mL', 'Set of 3 Wooden Treasure Chest Boxes, Vintage Antique Small Decorative Trunks', 'LONG TAO 2 Pcs 7.5" × 5" × 2.4" Wooden Succulent Planter Plant Container Box Rectangular Flower Pot']
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Unable to scrape similar links from different depth out of a webpage

I've created a script in python to parse different links from a webpage. There are two section in the landing page. One is Top Experiences and the other is More Experiences. My current attempt can fetch the links from both the categories.
The type of links I wanna collect are (few of them) under the Top Experiences section at this moment. However, when I traverse the links under More Experiences section, I can see that they all lead to the page in which there is a section named Experiences under which there are links that are similar to the links under Top Experiences in the landing page. I wanna grab them all.
One such desirable link I'm after looks like: https://www.airbnb.com/experiences/20712?source=seo.
website link
My current attempt fetches the links from both the categories:
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
URL = "https://www.airbnb.com/sitemaps/v2/experiences_pdp-L0-0"
def get_links(link):
res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
items = [urljoin(link,item.get("href")) for item in soup.select("div[style='margin-top:16px'] a._1f0v6pq")]
return items
if __name__ == '__main__':
for item in get_links(URL):
print(item)
How can I parse all the links under Top Experiences section along with the links under Experiences section that can be found upon traversing the links under More Experiences?
Please check out the image if anything unclear. I used a pen available in paint so the writing may be a little hard to understand.
The solution is slightly tricky. It can be achieved in several ways. The one I find most useful is use the links under More Experiences within get_links() function recursively. All the links under More Experiences have a common keyword _pdp-.
So, when you define condional statement within the function to make the links sieve through the function get_links() recursively then the else block will produces the desired links. Most important thing to notice is that all the desired links are within the class _1f0v6pq So the logic of getting the links is fairly easy .
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
URL = "https://www.airbnb.com/sitemaps/v2/experiences_pdp-L0-0"
def get_links(link):
res = requests.get(link)
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select("div[style='margin-top:16px'] a._1f0v6pq"):
if "_pdp-" in item.get("href"):
get_links(urljoin(URL,item.get("href")))
else:
print(urljoin(URL,item.get("href")))
if __name__ == '__main__':
get_links(URL)
Process:
Get all Top Experiences links
Get all More Experiences links
Send a request to all More Experiences links one by one and get the links under Experiences in each page.
The div under which the links are present are same for all the pages have the same class _12kw8n71
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
from time import sleep
from random import randint
URL = "https://www.airbnb.com/sitemaps/v2/experiences_pdp-L0-0"
res = requests.get(URL)
soup = BeautifulSoup(res.text,"lxml")
top_experiences= [urljoin(URL,item.get("href")) for item in soup.find_all("div",class_="_12kw8n71")[0].find_all('a')]
more_experiences= [urljoin(URL,item.get("href")) for item in soup.find_all("div",class_="_12kw8n71")[1].find_all('a')]
generated_experiences=[]
#visit each link in more_experiences
for url in more_experiences:
sleep(randint(1,10))#avoid blocking by putting some delay
generated_experiences.extend([urljoin(URL,item.get("href")) for item in soup.find_all("div",class_="_12kw8n71")[0].find_all('a')])
Notes:
Your required links will be present in three lists top_experiences , more_experiences and generated_experiences
I have added random delay to avoid getting blocked.
Not printing the lists as it will be too long.
top_experiences - 50 links
more_experiences - 299 links
generated_experiences -14950 links
Seem like both the "Top Experience" and "More experiences" links share the same class so you can just use .find_all to obtain the links.
import requests
#from urllib.parse import urljoin
from bs4 import BeautifulSoup
# URL to scrape
url = "https://www.airbnb.com/sitemaps/v2/experiences_pdp-L0-0"
# Make request and Initialize BS4 with request content
req = requests.get(url)
soup = BeautifulSoup(req.content, "lxml")
# Tag that contains "Top Experiences" and "More Experiences"
soup.find_all(class_="_l8g1fr")
# Test Code
#Prints title of links and the href
links = soup.find_all(class_="_l8g1fr")
for link in links:
print(link.find("a").get_text())
print(link.find("a").get('href'))
Refactor code to meet your coding paradigm.
You can scrape from the divs with class "_12kw8n71":
from bs4 import BeautifulSoup as soup
import requests
d = soup(requests.get('https://www.airbnb.com/sitemaps/v2/experiences_pdp-L0-0').text, 'html.parser')
a, b, *_ = d.find_all('div', {'class':'_12kw8n71'})
result = {'top_experiences':[[i.text, i['href']] for i in a.find_all('a')], 'more_experiences':[[i.text, i['href']] for i in b.find_all('a')]}
Output (Only top experiences and part of the links from more experiences, as the full output exceeds Stackoverflow's character limit):
{'top_experiences': [["#1 HOLLYWOOD SIGN TOUR - WORLD'S BEST", '/experiences/26790?source=seo'], ['**#1-COMEDY/INSTAGRAM HOLLYWOOD WALK**', '/experiences/94033?source=seo'], ["A Potter's Wheel in Brooklyn", '/experiences/139532?source=seo'], ['Absolute: Seoul Pub Crawl & Party', '/experiences/161886?source=seo'], ['Amsterdam Experience Cruise', '/experiences/145329?source=seo'], ['Argentine Tango classes in London', '/experiences/241771?source=seo'], ['Axe throwing Canadian Experience', '/experiences/372251?source=seo'], ['Be a chocolate maker for a day', '/experiences/161912?source=seo'], ['Chianti in a Glass', '/experiences/179469?source=seo'], ['Cooking class in the Chianti Hills', '/experiences/25122?source=seo'], ['Cycle and Snack through hidden Bangkok', '/experiences/315116?source=seo'], ['Discover Valpolicella', '/experiences/77468?source=seo'], ["Don't watch Soweto, be part of Soweto", '/experiences/243188?source=seo'], ['Explore Pompeii w/ an Archaeologist', '/experiences/176605?source=seo'], ['Explore backstreets with a historian', '/experiences/90528?source=seo'], ['Explore the hidden food gems of Athens', '/experiences/256850?source=seo'], ['Fall and Winter Forest Trail Rides', '/experiences/236040?source=seo'], ['Foodies of Palermo - street food tour', '/experiences/212673?source=seo'], ['Forge a silver ring with jewellers', '/experiences/209198?source=seo'], ['Handmade Family Pasta & Tiramisù', '/experiences/115906?source=seo'], ['Hidden Gems of Old Delhi', '/experiences/154177?source=seo'], ['Hike Runyon Canyon with a rescue dog', '/experiences/82265?source=seo'], ['Hike Table Mountain with a Guide!', '/experiences/137248?source=seo'], ['Kayak in La Jolla', '/experiences/132375?source=seo'], ['Lakehouse Jazz', '/experiences/53239?source=seo'], ["Lisbon's best flavors", '/experiences/64564?source=seo'], ["London's Top Sights Tour : Fun Guide!", '/experiences/203605?source=seo'], ['Make ramen noodles from scratch', '/experiences/61279?source=seo'], ['Mount Etna excursion and lava tubes', '/experiences/221142?source=seo'], ['Must Have L.A. Pictures!', '/experiences/59665?source=seo'], ['MySurf School Bali', '/experiences/153910?source=seo'], ['PASTAMANIA', '/experiences/103280?source=seo'], ['Paddle with the Penguins', '/experiences/112515?source=seo'], ['Paella Maestro', '/experiences/51311?source=seo'], ["Paris' Best Kept Secrets Tour", '/experiences/113388?source=seo'], ['Play, Walk & Feed Anteaters +Kinkajou', '/experiences/270405?source=seo'], ['Red Dunes Safari & BBQ #The Real Camp', '/experiences/105242?source=seo'], ['San José Chocolate Tasting Tour', '/experiences/280822?source=seo'], ['Snorkel in Manly with a local expert', '/experiences/66873?source=seo'], ['Sunrise SUP into Caves & Grottos', '/experiences/242911?source=seo'], ['Sushi-making Experience', '/experiences/53271?source=seo'], ['Swim with sea lions in their natural habitat', '/experiences/327235?source=seo'], ['The Drinking & Prohibition walk', '/experiences/102778?source=seo'], ['The Ultimate Harry Potter Walking Tour', '/experiences/188432?source=seo'], ['Tsukiji (Old) vs Toyosu (New) S.S Tour', '/experiences/71924?source=seo'], ['Wolf Encounter', '/experiences/47240?source=seo'], ['XXL Wild&Organic Roman food: Eat&Learn', '/experiences/161461?source=seo'], ['Your First Chinese Seal-engraving Work', '/experiences/166172?source=seo'], ['Под руку с духами по старой Москве', '/experiences/94558?source=seo'], ["⭐BIKE to taste the world's BEST TACOS!", '/experiences/75047?source=seo']], 'more_experiences': [[" Flying Trapeze - 'Ten Things I Hate About You' Tour", '/sitemaps/v2/experiences_pdp-L1-0'], ["'The Devil in the White City' Tour - 23:59 in Paris", '/sitemaps/v2/experiences_pdp-L1-1'], ["24h en Republica Dominicana - 80s Cover Band's Secret Gig[FUKUOKA]", '/sitemaps/v2/experiences_pdp-L1-2'], ['80s Nights - A Journey Through History', '/sitemaps/v2/experiences_pdp-L1-3'], ["A Journey for the Tastebuds, in Malaga - A Tea Sommelier's Zen Tea Party", '/sitemaps/v2/experiences_pdp-L1-4'], ['A Time for Home - A music walk with a french composer', '/sitemaps/v2/experiences_pdp-L1-5'], ['A musical walk in Salzburg - ANALOG PHOTOGRAPHY in DC! Shoot FILM!', '/sitemaps/v2/experiences_pdp-L1-6'], ['ANGEL, ENERGIA GASTRONOMICA ANCESTRAL! - Acuarela al aire libre en Poblenou', '/sitemaps/v2/experiences_pdp-L1-7'], ['Acupuncture in Historic Clinic - Afternoon tea at the Casavant Villa', '/sitemaps/v2/experiences_pdp-L1-8'], ["Afterwork dans un Atelier d'Artistes - Alpine Raft Expérience", '/sitemaps/v2/experiences_pdp-L1-9'], ['Alpine Tour Skiing Uphill/Downhill - Amélie en live!', '/sitemaps/v2/experiences_pdp-L1-10'], ['An African Dance & Cultural Experience - Another BKK', '/sitemaps/v2/experiences_pdp-L1-11'], ['Another Etna: Trekking, Food and Wine - Aprende o perfecciona bailes caribeños', '/sitemaps/v2/experiences_pdp-L1-12'], ['Aprende una actividad unica de Oaxaca - Around historical area with lovely dog', '/sitemaps/v2/experiences_pdp-L1-13'], ['Around the Neva & its embankments - Art tour Soumaya & Jumex Museums visit', '/sitemaps/v2/experiences_pdp-L1-14'], ['Art tour at Bellas Artes - Ascent to the crater of the Puracé volcano', '/sitemaps/v2/experiences_pdp-L1-15'], ['Asciende a la cima del Batea Mahuida - Atlas Mountains Day Trip: imlil Valley', '/sitemaps/v2/experiences_pdp-L1-16'], ['Atlas Mountains Full day Cultural Trip - Authentic Tango Night', '/sitemaps/v2/experiences_pdp-L1-17'], ['Authentic Tango with Live Music - BIARRITZ WALKING HISTORICAL TOUR', '/sitemaps/v2/experiences_pdp-L1-18'], ['BIG SUP Fun - Paddle Sevilla! - Bake ‘Dorayaki’ Japanese pancake', '/sitemaps/v2/experiences_pdp-L1-19'], ['Bakery Hop with Perth Brunch Bloggers - Banyuatis-Munduk Amazing Bike Tour', '/sitemaps/v2/experiences_pdp-L1-20'], ["Baptism of Rome for First-Timers - Batik: What's That?", '/sitemaps/v2/experiences_pdp-L1-21'], ['Batom vermelho - um estilo de vida - Beach Exercise Under The Sun', '/sitemaps/v2/experiences_pdp-L1-22'], ['Beach FlowJAM - Become Captain of the Day with Vicky', '/sitemaps/v2/experiences_pdp-L1-23'], ['Become Reiki 1 Certified in Hawaii! - Beginner Workshop', '/sitemaps/v2/experiences_pdp-L1-24'], ['Beginner and Kid Friendly Kayak Tour - Best Croissants + Coffee of Paris Walk', '/sitemaps/v2/experiences_pdp-L1-25'], ['Best Dirt Biking Experience in KL! - Big Easy Food Tours', '/sitemaps/v2/experiences_pdp-L1-26'], ['Big Fish In The Big Apple - Bike the Wineries', '/sitemaps/v2/experiences_pdp-L1-27'], ['Bike the local backroads - Birdwatching al Parco del Circeo', '/sitemaps/v2/experiences_pdp-L1-28'], ['Birdwatching and camping - Boat Trip at Arrabida with Lunch', '/sitemaps/v2/experiences_pdp-L1-29'], ['Boat Trip to historic Clonmacnoise - Boozin in Brooklyn: Spirits & Beer', '/sitemaps/v2/experiences_pdp-L1-30'], ["Boozy Icy Secrets - Breakfast Coffee Yoga # Kellogg's Cafe", '/sitemaps/v2/experiences_pdp-L1-31'], ['Breakfast With The (Chicago) Bear - Brugge voor jou.', '/sitemaps/v2/experiences_pdp-L1-32'], ['Brumby (wild) Horse Experience - Bushwalk with Bob', '/sitemaps/v2/experiences_pdp-L1-33'], ["Bushwalking; Airlie's best kept secret - Cabalgata, pasión por la aventura.", '/sitemaps/v2/experiences_pdp-L1-34'], ['Cabalgatas "Buscando el sol de ayer" - Camina por el parque observando aves', '/sitemaps/v2/experiences_pdp-L1-35'], ['Camina por la Historia de Jujuy - Canoeing with Champagne and Pastries', '/sitemaps/v2/experiences_pdp-L1-36'], ['Canoeing, Kayaking, Cycling in Catba - Capture your relationship in photos', '/sitemaps/v2/experiences_pdp-L1-37'], ['Capture your smile in beautiful hanbok - Catalonia Sailing Xperience', '/sitemaps/v2/experiences_pdp-L1-38'], ['Catalonia from Above - Cervantes Tapas Crawl Alcalá d Henares', '/sitemaps/v2/experiences_pdp-L1-39'], ['Cervezas artesanales de Querétaro - Cherry blossom viewing party in Ueno', '/sitemaps/v2/experiences_pdp-L1-40'], ['Cherry blossoms and your photo session - Chocolate tasting and tour', '/sitemaps/v2/experiences_pdp-L1-41'], ['Chocolate therapy - City Tour With Visit to Teatro Colon', '/sitemaps/v2/experiences_pdp-L1-42'], ['City Tour around Cali - Climb a sea cliff in El Empordà coast', '/sitemaps/v2/experiences_pdp-L1-43'], ['Climb an iconic hill with an instructor - Cocktails and Canapés', '/sitemaps/v2/experiences_pdp-L1-44'], ['Cocktails and Canvas - Columbia River King Salmon Fishing', '/sitemaps/v2/experiences_pdp-L1-45'], ['Columbus Cemetery. History and Art - Conhecendo o centro do recife', '/sitemaps/v2/experiences_pdp-L1-46'], ['Conhecendo o litoral e as belas praias - Cook great traditional dish with Heba', '/sitemaps/v2/experiences_pdp-L1-47'], ['Cook with Cristiana and Mamma Nora - Cook shelf mania.. authentic n savory', '/sitemaps/v2/experiences_pdp-L1-48'], ['Cook tapas at a secret private eatery - Cooking Spanish "tapas"', '/sitemaps/v2/experiences_pdp-L1-49'], ['Cooking Spanish classics with Soul! - Copenhagen Skyline Art Workshop', '/sitemaps/v2/experiences_pdp-L1-50'], ['Copenhagen Small Group Tour - Couture dans un atelier de Montmartre', '/sitemaps/v2/experiences_pdp-L1-51'], ["Covered Walkways Through a Writer’s Eyes - Crawlin' in the City - Bar Crawl", '/sitemaps/v2/experiences_pdp-L1-52'], ["Crazy French Sauces - Create a Story in a Writer's Retreat", '/sitemaps/v2/experiences_pdp-L1-53'], ['Create a Travel Journal - Create with a pro tie-dye artist', '/sitemaps/v2/experiences_pdp-L1-54'], ['Create wooden guitar picks and jewelry - Creative Raw Vegan Cooking Workshop', '/sitemaps/v2/experiences_pdp-L1-55'], ['Creative Rendezvous in Copenhagen - Créer ses cosmétiques naturels (DIY)', '/sitemaps/v2/experiences_pdp-L1-56'], ['Créer un Mandala Energétique - Cupcake Bake & Decorate - Spitalfields', '/sitemaps/v2/experiences_pdp-L1-57'], ['Cupcake Bouquet Class - Cycling in quiet NE. Connecticut.', '/sitemaps/v2/experiences_pdp-L1-58'], ['Cycling in the Don Valley - DJ WORKSHOP IN AACHEN GEBE DEN TAKT AN', '/sitemaps/v2/experiences_pdp-L1-59'], ['DJ Workshop and Indie Scene Night - Dark tour of Split', '/sitemaps/v2/experiences_pdp-L1-60'], ['Darkroom Photography in Kuala Lumpur - Decorative Wreath Workshop', '/sitemaps/v2/experiences_pdp-L1-61'], ['Decoupage for DIY memorabilia & gifts - Descubre México a través de la foto', '/sitemaps/v2/experiences_pdp-L1-62'], ['Descubre Playa Blanca, Saltos - Design a Garment in the World Market', '/sitemaps/v2/experiences_pdp-L1-63'], ['Design a game level in a game studio - Dinghy Sailing at El Portet', '/sitemaps/v2/experiences_pdp-L1-64'], ['Dingle Traditional Rowing - Discover CDMX from the best Rooftops', '/sitemaps/v2/experiences_pdp-L1-65'], ['Discover Caen "Art de vivre" - Discover Manzanita Scavenger Hunt', '/sitemaps/v2/experiences_pdp-L1-66'], ["Discover Marrakech Secrets by Bike - Discover Tarraco's living history", '/sitemaps/v2/experiences_pdp-L1-67'], ['Discover Tarragona coast from the sea - Discover nd paint your Aztec protector', '/sitemaps/v2/experiences_pdp-L1-68'], ['Discover new flavors with an oenophile - Discover the cosmos', '/sitemaps/v2/experiences_pdp-L1-69'], ['Discover the emotion of Kitesurf - Discovering Montjuic Hill', '/sitemaps/v2/experiences_pdp-L1-70'], ['Discovering Old Medina of Casablanca - Diving, Dipping, Swiming in Varadero', '/sitemaps/v2/experiences_pdp-L1-71'], ['Dj Workshop - Downtown Toronto Photoshoot Experience', '/sitemaps/v2/experiences_pdp-L1-72'], ['Downtown Traditional Cantina Pub Crawl - Drinking&Eating with Tokyohandsomeboy', '/sitemaps/v2/experiences_pdp-L1-73'], ['Drinks and a Show Walking Tour - Découverte des dauphins', '/sitemaps/v2/experiences_pdp-L1-74'], ['Découverte des spiritueux québécois - EL FASCINANTE MUNDO DEL MOSAICO', '/sitemaps/v2/experiences_pdp-L1-75'], ['ELDORADO: sus plantas y lugares. - Eat Like a Local - Dinner Experience', '/sitemaps/v2/experiences_pdp-L1-76'], ['Eat Like a Local at Butterworth - Eco Friendly Ranch & Petting Zoo', '/sitemaps/v2/experiences_pdp-L1-77'], ['Eco Hike in Pinar del Rio - Electric Skateboard down Paris', '/sitemaps/v2/experiences_pdp-L1-78'], ['Electric Skateboard the Highline Canal - Enigmatic / Sintra', '/sitemaps/v2/experiences_pdp-L1-79'], ['Enjoin Local Meal By Taking Vegetable - Enjoy local food experience in Tokyo!', '/sitemaps/v2/experiences_pdp-L1-80'], ['Enjoy local western part of Jeju ! - Escalada en Roca en Acantilados Valpo', '/sitemaps/v2/experiences_pdp-L1-81'], ['Escalada en Tarifa - Everyone Can Dance! Brussels', '/sitemaps/v2/experiences_pdp-L1-82'], ['Everyone can do Calligraphy - Experience Fabulous698B! Spring menu!', '/sitemaps/v2/experiences_pdp-L1-83'], ['Experience Falconry Firsthand - Experience a tasting menu in Saigon', '/sitemaps/v2/experiences_pdp-L1-84'], ['Experience a traditional Sunday lunch - Experiencia de golf en Buenos Aires', '/sitemaps/v2/experiences_pdp-L1-85'], ['Experiencia de un día en Salamanca - Explore Bowen Island Walking Adventure', '/sitemaps/v2/experiences_pdp-L1-86'], ['Explore Brazilian cuisine with a chef - Explore Korea broadcast Town & MBC', '/sitemaps/v2/experiences_pdp-L1-87'], ['Explore Korčula with Art historian - Explore Saratoga Passage by Kayak', '/sitemaps/v2/experiences_pdp-L1-88'], ['Explore Sceneries of Sokcho - Explore city nightlife with a musician', '/sitemaps/v2/experiences_pdp-L1-89'], ['Explore city nightlife with locals! - Explore the Old District Sham Shui Po', '/sitemaps/v2/experiences_pdp-L1-90'], ['Explore the Oslo Wilderness in Kayak - Exploring Maspalomas by E-Scooter', '/sitemaps/v2/experiences_pdp-L1-91'], ['Exploring RawaPening Lake of Salatiga - FOOTBALL MATCH IN MEDELLIN', '/sitemaps/v2/experiences_pdp-L1-92'], ['FOREST BATHING AND CREATING MEMORIES - Fantasy Photo~Shoot with A Unicorn...', '/sitemaps/v2/experiences_pdp-L1-93'], ['Fantasy adventure in the mountains - Fazendo Arte na Serra da Cantareira!', '/sitemaps/v2/experiences_pdp-L1-94'], ['Faça acrobacia em tecido como circense - Film your experience in Osaka', '/sitemaps/v2/experiences_pdp-L1-95'], ['FilmNeverDies-Filmphotography Workshop - Fishing with the Presidents', '/sitemaps/v2/experiences_pdp-L1-96'], ['Fishing, Malecon and Sunset - Florist in Paris', '/sitemaps/v2/experiences_pdp-L1-97'], ['Florist terrace & aperol spritz - Follow a historian through Havana', '/sitemaps/v2/experiences_pdp-L1-98'], ['Follow a local in vibrant East London - Food, Fotos & The French', '/sitemaps/v2/experiences_pdp-L1-99'], ['Food, Nature and Art on Tuscan Hills - Fossil Recovery Exploration', '/sitemaps/v2/experiences_pdp-L1-100'], ['Fossil hunting, Lets GO! - French food market tour & aperitif', '/sitemaps/v2/experiences_pdp-L1-101'], ['French macarons baking class - Full day surf experience', '/sitemaps/v2/experiences_pdp-L1-102'], ['Full day trail ride - Gaelic Craic and Cuisine', '/sitemaps/v2/experiences_pdp-L1-103'], ['Gain insight from a spiritual healer - Gems of Rome...', '/sitemaps/v2/experiences_pdp-L1-104'], ['Generations of Beekeeping - Get uplifted with gospel in Harlem', '/sitemaps/v2/experiences_pdp-L1-105'], ['Get ur Shine On Moonshine/Whiskey Tour - Glenorchy and Paradise Explorer', '/sitemaps/v2/experiences_pdp-L1-106'], ['Gliding on the Ice in the Red Dot - Goat Yoga!', '/sitemaps/v2/experiences_pdp-L1-107'], ['Goatherd for one day! - Graffiti, Street & Young Art in Vienna', '/sitemaps/v2/experiences_pdp-L1-108'], ['Grampians hike & waterfalls - Guide tours with E-bike in Menorca', '/sitemaps/v2/experiences_pdp-L1-109'], ['Guided Photo Tour ART Walk - Guitar workshop for the Byron Vibe', '/sitemaps/v2/experiences_pdp-L1-110'], ['Gulf Coast Birding For Beginners - Hand Building with clay in Brooklyn', '/sitemaps/v2/experiences_pdp-L1-111'], ['Hand Coffee Roasting & Cupping Class - Hang loose w/ Island Style Surf School', '/sitemaps/v2/experiences_pdp-L1-112'], ['Hang out in Shibuya and craft bonsai - Have a personal photographer for a day', '/sitemaps/v2/experiences_pdp-L1-113'], ['Have a picnic in a national park - Helicopter tour from Paris to Versailles', '/sitemaps/v2/experiences_pdp-L1-114'], ['Helicopter tour in São Paulo - Hidden Sculpture Stories Tour', '/sitemaps/v2/experiences_pdp-L1-115'], ['Hidden Sights & Cosmopolitan Magic - Hike Forest+ City Summit+ Rose Garden', '/sitemaps/v2/experiences_pdp-L1-116'], ['Hike Gracia hills for view & workout - Hike the Braids and Blackford Hill', '/sitemaps/v2/experiences_pdp-L1-117'], ['Hike the Bridge to Nowhere - Hiking Ajusco (1day)', '/sitemaps/v2/experiences_pdp-L1-118'], ['Hiking Ancient Mountain Olive Trees - Hill Country Estate Winery Tour', '/sitemaps/v2/experiences_pdp-L1-119'], ['Hill Country Yoga Hike - History and Tapas', '/sitemaps/v2/experiences_pdp-L1-120'], ['History and art inside Tijuca forest - Home-based cooking class in Panaji', '/sitemaps/v2/experiences_pdp-L1-121'], ['Home-cooked traditional Indian Food - Horseback ride California cowboy style', '/sitemaps/v2/experiences_pdp-L1-122'], ['Horseback ride and lunch with a farmer - Hungarian Cooking Class & Market Walk', '/sitemaps/v2/experiences_pdp-L1-123'], ['Hungarian pasta workshop and dinner - Ikebana experience', '/sitemaps/v2/experiences_pdp-L1-124'], ['Ikebana exprence - In the middle of the wet forest.', '/sitemaps/v2/experiences_pdp-L1-125'], ['In the trails of Rio - Inside Scoop LA', '/sitemaps/v2/experiences_pdp-L1-126'], ['Inside Showjumping with a Journalist - Interactive experience of lightpainting room', '/sitemaps/v2/experiences_pdp-L1-127'], ['Interactive outdoor navigation course - Invigorating Yoga Flow at Balboa Park', '/sitemaps/v2/experiences_pdp-L1-128'], ['Invitation To Ginza Personal Shopping - JUNGLE ART WALK TULUM', '/sitemaps/v2/experiences_pdp-L1-129'], ['JUNGLE IN THE CITY - Jazz in Cape Town with Lee Thomson', '/sitemaps/v2/experiences_pdp-L1-130'], ['Jazz in a Secret Venue - Joshua Tree Macramé Journey', '/sitemaps/v2/experiences_pdp-L1-131'], ['Joshua Tree Recording Studio & Lodging - Kamikochi hike and visit the Shrine', '/sitemaps/v2/experiences_pdp-L1-132'], ['Kanazawa Haul at Depachika - Kayak with the Dolphins', '/sitemaps/v2/experiences_pdp-L1-133'], ['Kayak&Snorkeling experience in Genoa - Kitesurf In Lake Garda', '/sitemaps/v2/experiences_pdp-L1-134'], ['Kitesurf Lecce-Scuola Kite Salento - Kundalini Yoga to awaken Inner Lover', '/sitemaps/v2/experiences_pdp-L1-135'], ['Kundalini Yoga, Gong and Yogi Tea - LGBTQ+ History Tour of Brighton', '/sitemaps/v2/experiences_pdp-L1-136'], ['LGBTQ+ queer and trans tour of London - Lamawandern im Mostviertel', '/sitemaps/v2/experiences_pdp-L1-137'], ['Lamb Farm Life - Learn 3D printing at Maker Bean Cafe!', '/sitemaps/v2/experiences_pdp-L1-138'], ['Learn 3D printing: design a souvenir - Learn Pencak Silat (Bali Martial Art)', '/sitemaps/v2/experiences_pdp-L1-139'], ['Learn Phone Photography in Mongkok - Learn and indulge in Quercy cuisine', '/sitemaps/v2/experiences_pdp-L1-140'], ['Learn and play poker in Barão Geraldo - Learn how to wakeboard', '/sitemaps/v2/experiences_pdp-L1-141'], ['Learn how to wakeboard or wakesurf - Learn to DJ and dance at a queer club', '/sitemaps/v2/experiences_pdp-L1-142'], ['Learn to DJ at Private Studio - Learn to bake sourdough in my kitchen', '/sitemaps/v2/experiences_pdp-L1-143'], ['Learn to bake traditional Irish Breads - Learn to play badminton!', '/sitemaps/v2/experiences_pdp-L1-144'], ['Learn to play poker from a Vegas pro - Legends and secrets with traditional music', '/sitemaps/v2/experiences_pdp-L1-145'], ["Lei Po'o (Flower Crown) Workshop - Let's learn how to wearing kimono", '/sitemaps/v2/experiences_pdp-L1-146'], ["Let's learn to SKETCH & DRAW! - Life changing Hike on Crowders Mtn.", '/sitemaps/v2/experiences_pdp-L1-147'], ['Life coaching with horses - Lisbon with the historic tram', '/sitemaps/v2/experiences_pdp-L1-148'], ["Lisbon's CatWalk - Living the Santa Cruz Canyon!", '/sitemaps/v2/experiences_pdp-L1-149'], ['Living the dying city! - London Chicken Wing Tour!', '/sitemaps/v2/experiences_pdp-L1-150'], ['London Christmas Lights Running Tour - Luis Barragán en 5 tiempos.', '/sitemaps/v2/experiences_pdp-L1-151'], ['Lujan de Cuyo Private Wine Tour - Macaron Class in Paris', '/sitemaps/v2/experiences_pdp-L1-152'], ["Macaron Masterclass in Borough - Makati's best food drinks and music", '/sitemaps/v2/experiences_pdp-L1-153'], ['Make Fujiyama Katsu-Curry with a chef - Make Wine Candles+Bottle Cutting (DIY)', '/sitemaps/v2/experiences_pdp-L1-154'], ['Make Your Leather Tortellino Keychain - Make a seashell frame after a boatride', '/sitemaps/v2/experiences_pdp-L1-155'], ['Make a signature scent with a perfumer - Make sushi in a traditional folk house', '/sitemaps/v2/experiences_pdp-L1-156'], ['Make sustainable leather espadrilles - Make your own silk scarf in Paris !', '/sitemaps/v2/experiences_pdp-L1-157'], ['Make your own silver jewel in Florence - Malen und Zeichnen', '/sitemaps/v2/experiences_pdp-L1-158'], ['Maleny Yoga Rainforest Wellness - Market Tour and a Home Cooked Meal', '/sitemaps/v2/experiences_pdp-L1-159'], ['Market Tour&Cook Kenyan cultural food - Matera Photo Journey', '/sitemaps/v2/experiences_pdp-L1-160'], ['Mathematics Walking Tour of Amsterdam - Meditazione rilassamento profondo', '/sitemaps/v2/experiences_pdp-L1-161'], ['Meditazione e benEssere in spiaggia - Men & Womens Wellness Retreat', '/sitemaps/v2/experiences_pdp-L1-162'], ['Mental Wellness Picnic Chat - Midday Vermouth Madrileño & Tortilla', '/sitemaps/v2/experiences_pdp-L1-163'], ['Middle Eastern Food Cooking Class - Mini Horse Experience!', '/sitemaps/v2/experiences_pdp-L1-164'], ['Mini Nature Retreat - Moniz Family Surf: Private Surf Lesson', '/sitemaps/v2/experiences_pdp-L1-165'], ['Moniz Family Surf: Waikiki Surf Lesson - Morocco in frames:Culture &photography', '/sitemaps/v2/experiences_pdp-L1-166'], ['Mortadella & Bologna: a love story - Mountain plateau hike (5-7 hr)', '/sitemaps/v2/experiences_pdp-L1-167'], ['Mountain summit and cozy restaurant - Music, Mummies, and Museums!', '/sitemaps/v2/experiences_pdp-L1-168'], ['Music, art, history, Rio nightlife - NEW! Amsterdam light cruise', '/sitemaps/v2/experiences_pdp-L1-169'], ['NEW! Aperitivo tour - Fun Fun Fun! ❤️ - National Arboretum and #madeinDC Tour', '/sitemaps/v2/experiences_pdp-L1-170'], ['National Gallery with an Art Historian - Nature trails & Birding near Cali', '/sitemaps/v2/experiences_pdp-L1-171'], ['Nature walk near Hotspring in Jozankei - Night Cheese: California Artisans', '/sitemaps/v2/experiences_pdp-L1-172'], ["Night Cherry Viewing - Nonna Cecilia's Pasta Workshop!", '/sitemaps/v2/experiences_pdp-L1-173'], ['Noodle & Roll & Water Puppet Theater - Obstacle Course Adventure', '/sitemaps/v2/experiences_pdp-L1-174'], ['Ocean Beach Coffee Cart Bike Tour - Old Montreal: Intimate & Personalized', '/sitemaps/v2/experiences_pdp-L1-175'], ['Old Quebec : Mesdames & Mesdemoiselles - Ontdek de ambacht van het jam maken', '/sitemaps/v2/experiences_pdp-L1-176'], ['OoLong Tea Tasting - Osaka Old City Walk Tour with Sweets!', '/sitemaps/v2/experiences_pdp-L1-177'], ['Osaka Shinsekai/Dotonbori Walking Tour - PASTA MATRICIANA MAKING CLASS', '/sitemaps/v2/experiences_pdp-L1-178'], ['PASTA grani antichi PANE lievito nat - Paddle with the Penguins', '/sitemaps/v2/experiences_pdp-L1-179'], ['Paddle your way in history - Paint a skateboard deck with an artist', '/sitemaps/v2/experiences_pdp-L1-180'], ['Paint a surfboard to send home! - Painting on the Amalfi Coast.', '/sitemaps/v2/experiences_pdp-L1-181'], ['Painting on the Pier - Paris fabrics & crafts', '/sitemaps/v2/experiences_pdp-L1-182'], ['Paris for Families - Passeio a cavalo no por do Sol', '/sitemaps/v2/experiences_pdp-L1-183'], ['Passeio agradável no bairro do Bexiga! - Pedal untamed back roads of Outaouais', '/sitemaps/v2/experiences_pdp-L1-184'], ['Pedala nelle pinete di Ravenna - Personal Shopping Consultant', '/sitemaps/v2/experiences_pdp-L1-185'], ['Personal Shopping For Bespoke Suits - Photo Session in beautiful Basel', '/sitemaps/v2/experiences_pdp-L1-186'], ['Photo Session in sunny Ibiza - Photo tour of Dublin city', '/sitemaps/v2/experiences_pdp-L1-187'], ["Photo tour on bike with an expert - Photographing in 'off-piste' Tokyo.", '/sitemaps/v2/experiences_pdp-L1-188'], ['Photographing in Palermo markets - Photoshoot for yoga lovers in Paris', '/sitemaps/v2/experiences_pdp-L1-189'], ['Photoshoot from Varadero to Havana - Pick plant n Cook,KANOM JAAK,dessert', '/sitemaps/v2/experiences_pdp-L1-190'], ['Pick wild apples and can applesauce - Pizza Class on a Renaissance Terrace', '/sitemaps/v2/experiences_pdp-L1-191'], ['Pizza Cooking Class in Milan - Play the Chinese ink art with Runkuan', '/sitemaps/v2/experiences_pdp-L1-192'], ['Play the Fiddle in Under an Hour - Por los Cerros de Santiago.', '/sitemaps/v2/experiences_pdp-L1-193'], ['Por los cementerios de Chepe. - Pose for pin-ups with a photographer', '/sitemaps/v2/experiences_pdp-L1-194'], ['Pose for portraits in Cambridge - Prague experience: archaeology&history', '/sitemaps/v2/experiences_pdp-L1-195'], ['Prague for Locals and You - Private Alpine Backpacking Trip', '/sitemaps/v2/experiences_pdp-L1-196'], ['Private Amalfi & Positano HalfDay tour - Private Yoga Class & Eco Snack', '/sitemaps/v2/experiences_pdp-L1-197'], ['Private Yoga Class for All Levels - Professional photos of your LA Vacay!', '/sitemaps/v2/experiences_pdp-L1-198'], ['Professional photoshoot by the sea - Pêche dans le Parc du Mercantour', '/sitemaps/v2/experiences_pdp-L1-199'], ['Pêche des Calamars en mer - Rafting the Isar', '/sitemaps/v2/experiences_pdp-L1-200'], ['Rafting trip-Snorkeling&cliff jumping - Recording Studio Magic (Immersive)', '/sitemaps/v2/experiences_pdp-L1-201'], ['Recording your own k-pop song - Relaxing Costarican Typical Trails', '/sitemaps/v2/experiences_pdp-L1-202'], ['Relaxing Countryside : Somewhere Snowy - Ride OFF Road in Puerto Rico', '/sitemaps/v2/experiences_pdp-L1-203'], ['Ride Retired Racehorses at Lake Fork - River hike captured on drone video', '/sitemaps/v2/experiences_pdp-L1-204'], ['River view cooking with super mom chef - Romancing the Bean', '/sitemaps/v2/experiences_pdp-L1-205'], ['Romantic "Aperitivo" with lake view - Rugged North Cascades Private Day Tour', '/sitemaps/v2/experiences_pdp-L1-206'], ['Ruin Pub Crawl with a Local Hostess - Running Tour of Brooklyn!', '/sitemaps/v2/experiences_pdp-L1-207'], ['Running Tour of San Diego - SAORI freestyle weaving for everyone', '/sitemaps/v2/experiences_pdp-L1-208'], ['SASHIKO; Learn the Japanese embroidery - SUP around Balboa Isle & the Back Bay', '/sitemaps/v2/experiences_pdp-L1-209'], ['SUP at sunset - Sail around the city with a boat lover', '/sitemaps/v2/experiences_pdp-L1-210'], ['Sail at Sunset in Porto - Sails, Whales, and Trails', '/sitemaps/v2/experiences_pdp-L1-211'], ['Sailtour Lake Lucerne - Samurai Kendo Experience', '/sitemaps/v2/experiences_pdp-L1-212'], ['Samurai Ninja Experience & Guided Tour - Sardinia: sip by sip', '/sitemaps/v2/experiences_pdp-L1-213'], ['Sardinian identity in the kitchen - Scotch Whisky & Cheese Tasting', '/sitemaps/v2/experiences_pdp-L1-214'], ['Scotland Adventure Adrenaline Day - Seaside Yoga', '/sitemaps/v2/experiences_pdp-L1-215'], ['Seaside bike ride and paddle surfing - Secretos de la Perla Tapatía', '/sitemaps/v2/experiences_pdp-L1-216'], ['Secretos del centro de Coyoacán - Segeln in Berlin', '/sitemaps/v2/experiences_pdp-L1-217'], ['Segeltörn auf dem Steinhuder Meer - Seville From The Rooftops', '/sitemaps/v2/experiences_pdp-L1-218'], ['Seville Great Experience in E-Bike - Shmurgle a Goat, Cosy Winter Edition', '/sitemaps/v2/experiences_pdp-L1-219'], ['Shochu tasting workshop in Asakusa - Shop-til-you-drop with Fashion Stylist', '/sitemaps/v2/experiences_pdp-L1-220'], ['Shopper bag making class - Silicon Valley High Tech Company Tour', '/sitemaps/v2/experiences_pdp-L1-221']]}

Scrape Google News with lxml and python

I'm trying to scrape Google News using python and lxml. Everything is going well but when I try to print each div data using a for loop everything mess up.
Here my code:
# -*- coding: utf-8 -*-
from stem import Signal
from stem.control import Controller
from lxml import html
from lxml import cssselect
from lxml import etree
import requests
proxies = {
'http' : 'http://127.0.0.1:8123'
}
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
}
url = "https://www.google.it/search?hl=en&tbm=nws&as_occt=any&tbs=cdr:1,cd_min:9/1/2014,cd_max:9/1/2014,sbd:1&as_nsrc=Daily%20Mail&start=0"
page = requests.get(url,proxies=proxies,headers=headers)
tree = html.fromstring(page.content)
results = tree.xpath('//div[#class="_cnc"]')
for div in results:
print(div)
I get this output:
<Element div at 0x7f4154df9470>
<Element div at 0x7f4154df94c8>
<Element div at 0x7f4154df9520>
<Element div at 0x7f4154df9578>
<Element div at 0x7f4154df95d0>
<Element div at 0x7f4154df9628>
<Element div at 0x7f4154df9680>
<Element div at 0x7f4154df96d8>
<Element div at 0x7f4154df9730>
<Element div at 0x7f4154df9788>
I would like to extract from each div -> title, href and snippet, with something like this:
....
for div in results:
title = div.xpath('//a[#class="l _HId"]/text()')
href = div.xpath('//a[#class="l _HId"]/#href')
snippet = div.xpath('//div[#class="st"]/text()')
#for example
print(title)
....
When I try to print I get same multiple output:
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
['Pro-Russian rebels lower demands in peace talks', "'If I want to I can take Kiev in a fortnight': Putin's threat to Europe ", 'Showing a yen for business: PM Modi and Japanese premier Abe ', 'Modi visit draws pledges of support from Japan', 'The spectre of the Army rises again over Pakistan', 'Protesters briefly storm Pakistan state TV station', 'Anti-government protesters storm Pakistan state television station ', 'Austerity debate flares as Europe recovery fades', 'Inquiries begin into nude celebrity photo leaks', 'He tried to sell intimate pictures of Jennifer Lawrence in return for ']
Does anyone know what's wrong with my code?
You are almost there - just prepend the dots to the inner XPath expressions to make them specific to the context of the current node:
for div in results:
title = div.xpath('.//a[#class="l _HId"]/text()')
href = div.xpath('.//a[#class="l _HId"]/#href')
snippet = div.xpath('.//div[#class="st"]/text()')
#for example
print(title)

Unable to remove escape charactors from the print

Hi I am trying to extract information to put into a list containing plain text but can't find a way to remove the escape characters.
I am very new to python and programming in general. I have been trying to solve this but unable to find one.
This is my code:
import urllib
import re
from bs4 import BeautifulSoup
x=1
while x<2:
url = "http://search.insing.com/ts/food-drink/bars-pubs/bars-pubs?page=" +str(x)
htmlfile = urllib.urlopen(url).read()
soup = BeautifulSoup(htmlfile.decode('utf-8','ignore'))
reshtml = soup.find("div", "results").find_all("h3")
reslist = []
for item in reshtml:
res = item.get_text()
reslist.append(res)
print reslist
x += 1
seems like you're really after the text in the anchor there, consider changing
reshtml = soup.find("div", "results").find_all("h3")
to:
reshtml = [h3.a for h3 in soup.find("div", "results").find_all("h3")]
also change:
reslist.append(res)
to:
reslist.append(' '.join(res.split()))
here is what I get after changing:
[u'Parco Caffe', u'AdstraGold Microbrewery & Bistro Bar',
u'Alkaff Mansion Ristorante', u'The Fat Cat Bistro', u'Gravity Bar',
u'The Wine Company (Evans Road)', u'Serenity Spanish Bar & Restaurant (VivoCity)',
u'The New Harbour Cafe & Bar', u'Indian Times', u'Sunset Bay Beach Bar',
u'Friends # Jelita', u'Talk Cock Sing Song # Thomson',
u'En Japanese Dining Bar (UE Square)', u'Magma German Wine Bistro',
u"Tam Kah Shark's Fin", u'Senso Ristorante & Bar',
u'Hard Rock Cafe (HPL House)', u'St. James Power Station',
u'The St. James', u'Brotzeit German Bier Bar & Restaurant (Vivocity)']
The current output looks like this:
[u'\n\r\n Parco Caffe\n',
u'\n\r\n AdstraGold Microbrewery & Bistro Bar\n',
u'\n\r\n Alkaff Mansion Ristorante\n',
u'\n\r\n The Fat Cat Bistro\n',
u'\n\r\n Gravity Bar\n',
u'\n\r\n The Wine Company\r\n (Evans Road)\r\n \n',
u'\n\r\n Serenity Spanish Bar & Restaurant\r\n (VivoCity)\r\n \n',
u'\n\r\n The New Harbour Cafe & Bar\n',
u'\n\r\n Indian Times\n',
u'\n\r\n Sunset Bay Beach Bar\n',
u'\n\r\n Friends # Jelita\n',
u'\n\r\n Talk Cock Sing Song # Thomson\n',
u'\n\r\n En Japanese Dining Bar\r\n (UE Square)\r\n \n',
u'\n\r\n Magma German Wine Bistro\n',
u"\n\r\n Tam Kah Shark's Fin\n",
u'\n\r\n Senso Ristorante & Bar\n',
u'\n\r\n Hard Rock Cafe\r\n (HPL House)\r\n \n',
u'\n\r\n St. James Power Station \n',
u'\n\r\n The St. James\n',
u'\n\r\n Brotzeit German Bier Bar & Restaurant\r\n (Vivocity)\r\n \n']
Adding these lines before the print:
reslist = [y.replace('\n','').replace('\r','') for y in reslist]
reslist = [y.strip() for y in reslist]
Gives me this output:
[u'Alkaff Mansion Ristorante',
u'Parco Caffe',
u'AdstraGold Microbrewery & Bistro Bar',
u'Gravity Bar',
u'The Fat Cat Bistro',
u'The Wine Company (Evans Road)',
u'Serenity Spanish Bar & Restaurant (VivoCity)',
u'The New Harbour Cafe & Bar',
u'Indian Times',
u'Sunset Bay Beach Bar',
u'Friends # Jelita',
u'Talk Cock Sing Song # Thomson',
u'En Japanese Dining Bar (UE Square)',
u'Magma German Wine Bistro',
u"Tam Kah Shark's Fin",
u'Senso Ristorante & Bar',
u'Hard Rock Cafe (HPL House)',
u'St. James Power Station',
u'The St. James',
u'Brotzeit German Bier Bar & Restaurant (Vivocity)']
Is that what you are looking for?
Guy's answer is much better, and more BeautifulSoup specific.

Categories