Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Web-scraping, multiple webpages
#1
Information 
Hello there,

I'm a newbie and still learning python and I have some problem with my scraping code. Namely, I would like to scrap data from 273 pages of the same website . My code works when I want to scrap one page, but when I add more pages to code it scrapes only from the last one. There is code below:

import requests
import bs4
from bs4 import BeautifulSoup
headers = {
    'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}
page = 1
while page !=274:
    url=f"https://www.kmotorshop.com/en/article-list/list/0/0/tree-shop%7C258?page={page}&itemsPerPage=50&style=1"
    page = page + 1
r = requests.get(url,{'headers':headers})
soup = bs4.BeautifulSoup(r.text,'html.parser')
i = 0
while i != 50:
    print (soup.findAll('div',{'class': 'product-line__heading'})[i].find('a').text)
    i+=1
Can someone help me to connect all pages and scrape them all together?
Yoriz write Dec-28-2022, 02:04 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
In the while loop, the variable url is overwritten each time it loops leaving it with only the last value,
When using variables in loops you could either interact with the current item ieurl inside of the loop or use something that can contain multiple items like a list
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  scraping multiple pages from table bandar 1 2,709 Jun-27-2020, 10:43 PM
Last Post: Larz60+
  Scraping Multiple Pages mbadatanut 1 4,237 May-08-2020, 02:30 AM
Last Post: Larz60+
  Scraping from multiple URLS to print in a single line. jb89 4 3,373 Jan-29-2020, 06:12 AM
Last Post: perfringo
  MaxRetryError while scraping a website multiple times kawasso 6 17,482 Aug-29-2019, 05:25 PM
Last Post: kawasso
  Downloading Multiple Webpages MoziakBeats 4 3,266 Apr-17-2019, 04:06 AM
Last Post: Skaperen
  scraping with multiple iframe jansky 1 4,217 Nov-09-2018, 11:12 AM
Last Post: snippsat
  scraping multiple pages of a website. Blue Dog 14 22,438 Jun-21-2018, 09:03 PM
Last Post: Blue Dog
  Scraping data from a web page where same class name applied multiple times sumandas89 1 16,057 Dec-30-2017, 11:03 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020