Issue
I am trying to scrape multiple pages, but the following code scrapes only one page. How can I scrape the other pages?
import requests
from bs4 import BeautifulSoup
headers ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}
for page in range(0, 10):
r =requests.get('https://www.ebay.com/sch/i.html?_from=R40&_nkw=sneakers&_sacat=0&_pgn={}".format(page * 10)')
soup=BeautifulSoup(r.content, 'lxml')
tags= soup.find_all('li', attrs={'class': 's-item'})
for pro in tags:
title=pro.find('h3',class_='s-item__title').text.encode("utf-8")
price=pro.find('div',class_='s-item__detail s-item__detail--primary').text.encode("utf-8")
print(title,price)
Solution
Now using correct locator, I'm getting working output:
Code:
import requests
from bs4 import BeautifulSoup
import pandas as pd
Title = []
p = []
headers ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
}
for page in range(0, 10):
r =requests.get('https://www.ebay.com/sch/i.html?_from=R40&_nkw=sneakers&_sacat=0&_pgn={page}'.format(page=page), headers = headers)
soup=BeautifulSoup(r.content, 'lxml')
tags= soup.find_all('div', attrs={'class': 's-item__info clearfix'})
for pro in tags:
title=pro.find('h3',class_='s-item__title').text
Title.append(title)
price =pro.find('div',class_='s-item__detail s-item__detail--primary').text
p.append(price)
#print(title,price)
df = pd.DataFrame(
{"Title": Title, "Price": p}
)
print(df)
Output:
Title Price
0 7S0ponso rPA Eed-1 UJ 0F -1-1
1 Converse CHUCK TAYLOR All Star Low Top Unisex ... $38.95 to $64.95
2 Air Jordan 1 Mid University Gold White Black Y... $130.00
3 Air Jordan 1 Mid Metallic Red Gym Red Black Wh... $149.95
4 Nike Air Force 1 Low Triple White ‘07 BRAND NE... $99.99
.. ... ...
465 Salomon S-lab XT 6 Soft Ground Size Men 11 US $50.00
466 Nike Dunk Low Light Bone Tropical Twist (GS) -... $170.99
467 Nike Air Force 1 '07 Shoes Black Men's Multi S... $100.00
468 12 Mens 13.5 W Reebok Classic Harman Run S Pri... $47.99
469 NIKE SHOX ZOOM AIR Mid High Basketball Shoes ... $50.00
[470 rows x 2 columns]
Answered By - Fazlul
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.