The code is as:
`
import time
from bs4 import BeautifulSoup
import requests
import csv
def get_page(url):
response = requests.get(url)
if not response.ok:
print("Server Responded: ", response.status_code)
else:
soup = BeautifulSoup(response.text, "lxml")
return soup
def get_detail_data(soup):
#title
#price
#imageURL
try:
title = soup.find("h1", class_="x-item-title__mainTitle").find("span").get_text().strip()
print(title)
except:
title = ""
try:
price = soup.find("div", class_="x-price-primary").find("span").get_text().strip()
print(price)
except:
price = ""
try:
image_url = soup.find("div", class_="ux-image-carousel-item image-treatment active image").find("img").get("src").strip()
print(image_url)
except:
image_url = ""
data = {
'Title': title,
'Price': price,
'Image URL': image_url
}
return data
def get_index_data(soup):
try:
links = soup.find_all("a", class_="s-item__link").strip()
except:
links = []
urls = [item.get("href") for item in links]
print(urls)
return urls
def main():
url = "https://www.ebay.com/sch/i.html?_from=R40&_nkw=watch+men&_sacat=260324&_pgn=1"
products = get_index_data(get_page(url))
for link in products:
print(link)
data = get_detail_data(get_page(link))
print(data)
if name == “main“:
main()
`
I was trying to scrape watches section from Ebay from search functionality. It works individually for a single page of watch but when I try to seach using a for loop as done above. It returns the error :
Traceback (most recent call last):
File “/Users/shivanshu/Documents/Projects/WebScrapers/Ebay/ebay_scraper.py”, line 84, in
main()
File “/Users/shivanshu/Documents/Projects/WebScrapers/Ebay/ebay_scraper.py”, line 80, in main
data = get_detail_data(get_page(link))
^^^^^^^^^^^^^^
File “/Users/shivanshu/Documents/Projects/WebScrapers/Ebay/ebay_scraper.py”, line 14, in get_page
return soup
^^^^
Shivanshu Dwivedi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.