-1

I am trying to web scrape a forum using python which has multiple pages.

Example :https://www.f150forum.com/f118/would-you-buy-f150-again-463954/

How can I go to multiple pages of the website to extract comments from each user? Thank you in advance

url = "https://www.f150forum.com/f118/would-you-buy-f150-again-463954/"
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.text, 'html.parser')
domains = soup.find_all("div")
posts = soup.find(id = "posts")
comments_class = soup.findAll('div',attrs={"class":"ism-true"})   
comments = [row.get_text() for row in comments_class]

anonymous13
  • 581
  • 1
  • 5
  • 17

1 Answers1

1
import requests
from bs4 import BeautifulSoup
import re

data = []
with requests.Session() as req:
    for item in range(1, 11):
        print(f"Extracting Page# {item}")
        r = req.get(
            f"https://www.f150forum.com/f118/would-you-buy-f150-again-463954/index{item}/")
        soup = BeautifulSoup(r.text, 'html.parser')
        result = [item.get_text(strip=True, separator=" ") for item in soup.findAll(
            "div", id=re.compile("^post_message_"))]
        data.append(result)

print(data)