简体   繁体   中英

Moving to the next page using BeautifulSoup for scraping

I would need to scrape the content (just titles) from a website. I did it for one page, but I would need to do it for all the pages on the website. Currently, I am doing as follows:

import bs4, requests
import pandas as pd
import re

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
    
    
r = requests.get(website, headers=headers)
soup = bs4.BeautifulSoup(r.text, 'html')


title=soup.find_all('h2')

I know that, when I move to the next page, the url changes as follows:

website/page/2/
website/page/3/
... 
website/page/49/
...

I tried to build a recursive function using next_page_url = base_url + next_page_partial but it does not move to the next page.

if soup.find("span", text=re.compile("Next")):
    page = "https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/".format(page_num)
    page_num +=10 # I should scrape all the pages so maybe this number should be changed as I do not know at the beginning how many pages there are for that section
    print(page_num)
else:
    break

I followed this question (and answer): Moving to next page for scraping using BeautifulSoup

Please let me know if you need more info. Many thanks

Updated code:

import bs4, requests
import pandas as pd
import re

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}

page_num=1
website="https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina"

while True:
  r = requests.get(website, headers=headers)
  soup = bs4.BeautifulSoup(r.text, 'html')


  title=soup.find_all('h2')

  if soup.find("span", text=re.compile("Next")):
      page = f"https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{page_num}".format(page_num)
      page_num +=10
  else:
      break

If you use f"url/{page_num}" then remove format(page_num) .

You can use anything you want below:

page = f"https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{page_num}"

or

page = "https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{}".format(page_num)

Good luck!


Final answer will be this:

import bs4, requests
import pandas as pd
import re

headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}

page_num=1
website="https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina"

while True:
  r = requests.get(website, headers=headers)
  soup = bs4.BeautifulSoup(r.text, 'html')


  title=soup.find_all('h2')

  if soup.find("span", text=re.compile("Next")):
      website = f"https://catania.liveuniversity.it/notizie-catania-cronaca/cronacacatenesesicilina/page/{page_num}"
      page_num +=1
  else:
      break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM