Skip to content Skip to sidebar Skip to footer

Limited Number Of Scraped Data?

I am scraping a website and everything seems work fine from today's news until news published in 2015/2016. After these years, I am not able to scrape news. Could you please tell m

Solution 1:

import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
import pandas as pd


def main(req, num):
    r = req.get(
        "https://catania.liveuniversity.it/attualita/page/{}/".format(num))
    soup = BeautifulSoup(r.content, 'html.parser')
    try:
        data = [(x.select_one("span.updated").text, x.findAll("a")[1].text, x.select_one("div.entry-content").get_text(strip=True)) for x in soup.select(
            "div.col-lg-8.col-md-8.col-sm-8")]
        return data
    except AttributeError:
        print(r.url)
        return False


with ThreadPoolExecutor(max_workers=30) as executor:
    with requests.Session() as req:
        fs = [executor.submit(main, req, num) for num in range(1, 673)]
        allin = []
        for f in fs:
            f = f.result()
            if f:
                allin.extend(f)
        df = pd.DataFrame.from_records(
            allin, columns=["Date", "Title", "Content"])
        print(df)
        df.to_csv("result.csv", index=False)

Post a Comment for "Limited Number Of Scraped Data?"