我正在尝试编写一个程序来刮除eBay以便向我展示产品清单,但是打印到终端上的所有内容都是“无”。我期待产品的标题。
我已将代码更改为“查找”不同的关键字,但是输出始终是相同的“无”,通常这意味着它找不到指定的关键字,但是我输入的关键字显示在网络上页面和“检查元素”中。
以下是代码段:
<nav class="navbar navbar-expand-sm navbar-menu navbar-inverse">
<div class="container-fluid">
<div class="row navbar-row">
<a class="navbar-brand hvr-glow" href="index.html" target="_blank">
<img src="../assets/images/the-who-name-logo.png" class="menu-text hvr-glow" width="90" height="90" padding="5px" alt="Logo">
</a>
<button class="navbar-toggler menu-button" data-toggle="collapse" data-target="#mainNav" aria-controls="navbar-collapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="mainNav">
<div class="navbar-nav list-inline-block">
<a class="nav-item nav-link hvr-glow menu-text" href="index.html" target="_blank">Home</a>
<a class="nav-item nav-link hvr-glow menu-text" href="media.html" target="_blank">Media</a>
<a class="nav-item nav-link hvr-glow menu-text" href="contact.html" target="_blank">Contact</a>
</div>
</div>
</div>
</div>
</nav>
</div>
我试图仅获取与我在程序中输入的关键字相对应的结果。
答案 0 :(得分:0)
可能选择器有误。我能够使用此代码从 eBay HTML 中提取标题。
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)
输出
['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"]