I have the following script which scraps all the information from a website. However, when I run it, I get duplicated records of the blogs.
import requests
import pandas as pd
import numpy as np
from time import sleep
from random import randint
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import re
blog_topics = []
page = "https://www.bartonassociates.com/blog/"
soup = BeautifulSoup(requests.get(page).content, 'html.parser')
for link in soup.find_all(href=re.compile("/blog/tag")):
url = link.get('href')
if '/blog/tag/p' not in urlparse(link.get('href')).path:
blog_topics.append(url)
else:
pass
# VARIABLE TO DEFINE A RANGE BASED ON NO.OF PAGES
pages = np.arange(1)
# DEFINING CUSTOM VARIABLES
title_blognames_links_ = []
author_and_dates_ = []
# LOOP TO RETRIEVE TITLE, BLOG NAMES, LINKS, AUTHORS AND DATE PUBLISHED
for page in pages:
for blogs in blog_topics:
blog_url= blogs +'/p' + str(page)
sleep(randint(2,7))
soup = BeautifulSoup(requests.get(blog_url).content, 'html.parser')
#Information on title, blog names and their links
for h4 in soup.select("h4"):
for h2 in soup.select("h2"):
title_blognames_links_.append((h4.get_text(strip=True), h4.a["href"], h2.get_text(strip=True).replace('"',"")[11:]))
#Information of authors and dates
for tag in soup.find_all(class_="author"):
author_and_dates_.append(tag.get_text(strip=True))
I believe it has to do something with the pages = np.arange(1) range I have provided.
P.S. (1) was just a trail. I have tried (1,17),(1),(2)
Background: The maximum pages in a blog topic I have is 17, each topic has 10 blogs each (approx)
What I am looking for, is to get all unique blog information from all the blog topics
Not sure what I am doing wrong here
