Python Forum
Problem With Simple Multiprocessing Script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem With Simple Multiprocessing Script
#1
Is it possible to multiprocess this script without breaking it up into functions?

I'm trying to keep it as barebones and simple as possible.

# EXTREMELY SIMPLE SCRAPING SCRIPT


from time import sleep
from bs4 import BeautifulSoup
import re
import requests
from multiprocessing import Pool


exceptions = []

list1 = ["http://www.wallstreetinvestorplace.com/2018/04/cvs-health-corporation-cvs-to-touch-7-54-earnings-growth-for-next-year/",
         "https://macondaily.com/2018/04/06/cetera-advisors-llc-lowers-position-in-cvs-health-cvs.html",
         "http://www.thesportsbank.net/football/liverpool/jurgen-klopp-very-positive-about-mo-salah-injury/",
         "https://www.moneyjournals.com/trump-wasting-time-trying-bring-amazon/",
         "https://www.pmnewsnigeria.com/2018/04/06/fcta-targets-800000-children-for-polio-immunisation/",
         "http://toronto.citynews.ca/2018/04/06/officials-in-canada-braced-for-another-spike-in-illegal-border-crossings/",
         "https://www.pmnewsnigeria.com/2018/04/04/pdp-describes-looters-list-as-plot-to-divert-attention/",
         "https://beyondpesticides.org/dailynewsblog/2018/04/epa-administrator-pruitt-colluding-regulated-industry/",
         "http://thyblackman.com/2018/04/06/robert-mueller-is-searching-for/",
         "https://www.theroar.com.au/2018/04/06/2018-commonwealth-games-swimming-night-2-finals-live-updates-results-blog/",
         "https://medicalresearch.com/pain-research/migraine-linked-to-increased-risk-of-heart-disease-and-stroke/40858/",
         "http://www.investingbizz.com/2018/04/amazon-com-inc-amzn-stock-creates-investors-concerns/",
         "https://stocknewstimes.com/2018/04/06/convergence-investment-partners-llc-grows-position-in-amazon-com-inc-amzn.html",
         "https://factsherald.com/old-food-rules-needs-to-be-updated/",
         "https://www.nextadvisor.com/blog/2018/04/06/the-facebook-scandal-evolves/",
         "http://sacramento.cbslocal.com/2018/04/04/police-family-youtube-shooter/",
         "http://en.brinkwire.com/245768/why-does-stress-lead-to-weight-gain-study-sheds-light/",
         "https://www.marijuana.com/news/2018/04/monterey-bud-jeff-sessions-is-on-the-wrong-side-of-history-science-and-public-opinion/",
         "http://www.stocksgallery.com/2018/04/06/jpmorgan-chase-co-jpm-noted-a-price-change-of-0-80-and-amazon-com-inc-amzn-closes-with-a-move-of-2-92/",
         "https://stocknewstimes.com/2018/04/06/front-barnett-associates-llc-has-2-41-million-position-in-cvs-health-corp-cvs.html",
         "http://www.liveinsurancenews.com/colorado-mental-health-insurance-bill-to-help-consumers-navigate-the-system/",
         "http://newyork.cbslocal.com/2018/04/04/youtube-headquarters-shooting-suspect/",
         "https://ledgergazette.com/2018/04/06/liberty-interactive-co-series-a-liberty-ventures-lvnta-shares-bought-by-brandywine-global-investment-management-llc.html",
         "http://bangaloreweekly.com/2018-04-06-city-holding-co-invests-in-cvs-health-corporation-cvs-shares/",
         "https://www.thenewsguru.com/didnt-know-lawyer-paid-prostitute-130000-donald-trump/",
         "http://www.westlondonsport.com/chelsea/football-wls-conte-gives-two-main-reasons-chelseas-loss-tottenham",
         "https://registrarjournal.com/2018/04/06/amazon-com-inc-amzn-shares-bought-by-lenox-wealth-management-inc.html",
         "http://www.businessdayonline.com/1bn-eca-withdrawal-commence-action-president-buhari-pdp-tasks-nass/",
         "http://www.thesportsbank.net/football/manchester-united/pep-guardiola-asks-for-his-fans-help-vs-united-in-manchester-derby/",
         "https://www.pakistantoday.com.pk/2018/04/06/three-palestinians-martyred-as-new-clashes-erupt-along-gaza-border/",
         "http://www.nasdaqfortune.com/2018/04/06/risky-factor-of-cvs-health-corporation-cvs-is-observed-at-1-03/",
         "https://stocknewstimes.com/2018/04/06/cetera-advisor-networks-llc-decreases-position-in-cvs-health-cvs.html",
         "http://nasdaqjournal.com/index.php/2018/04/06/planet-fitness-inc-nyseplnt-do-analysts-think-you-should-buy/",
         "http://www.tv360nigeria.com/apc-to-hold-national-congress/",
         "https://www.pmnewsnigeria.com/2018/04/03/apc-governors-keep-sealed-lips-after-meeting-with-buhari/",
         "https://www.healththoroughfare.com/diet/healthy-lifestyle-best-foods-you-should-eat-for-weight-loss/7061",
         "https://stocknewstimes.com/2018/04/05/amazon-com-inc-amzn-shares-bought-by-west-oak-capital-llc.html",
         "http://www.current-movie-reviews.com/48428/dr-oz-could-you-be-a-victim-of-sexual-assault-while-on-vacation/",
         "https://www.brecorder.com/2018/04/07/410124/world-health-day-to-be-observed-on-april-7/",
         "http://www.coloradoindependent.com/169637/trump-pruitt-emissions-epa-pollution",
         "https://thecrimereport.org/2018/04/05/will-sessions-new-justice-strategy-turn-the-clock-back-on-civil-rights/",
         "http://en.brinkwire.com/245490/pasta-unlikely-to-cause-weight-gain-as-part-of-a-healthy-diet/"]

list_counter = 0

p = Pool(10)  # process count
records = p.map(,list1[list_counter])  # argument required
p.terminate()
p.join()

print()
print('Total URLS:', len(list1), "- Starting Task...")
print()

for items in list1:

    try:

        scrape = requests.get(list1[list_counter],
                              headers={"user-agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"},
                              timeout=10)

        if scrape.status_code == 200:

            html = scrape.content
            soup = BeautifulSoup(html, 'html.parser')

            """ --------------------------------------------- """
            # ---------------------------------------------------
            '''           --> SCRAPE ALEXA RANK: <--          '''
            # ---------------------------------------------------
            """ --------------------------------------------- """

            sleep(0.15)
            scrape = requests.get("http://data.alexa.com/data?cli=10&dat=s&url=" + list1[list_counter],
                                  headers={"user-agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36"})
            html = scrape.content
            soup = BeautifulSoup(html, 'lxml')

            rank = re.findall(r'<popularity[^>]*text="(\d+)"', str(soup))

            print("Server Status:", scrape.status_code, '-', u"\u2713", '-', list_counter, '-', list1[list_counter], '-', "Rank:", rank[0])

            list_counter = list_counter + 1

        else:
            print("Server Status:", scrape.status_code)
            list_counter = list_counter + 1
            pass

    except BaseException as e:
        exceptions.append(e)
        print()
        print(e)
        print()
        list_counter = list_counter + 1
        pass

if len(exceptions) > 0:
    print("OUTPUT ERROR LOGS:", exceptions)
else:
    print("No Errors To Report")
Reply


Messages In This Thread
Problem With Simple Multiprocessing Script - by digitalmatic7 - Apr-09-2018, 06:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple flask rest api problem cancerboi 4 2,828 Jan-29-2020, 03:10 PM
Last Post: brighteningeyes
  requests problem in python script "content type" abdlwafitahiri 4 3,216 Dec-29-2019, 02:29 PM
Last Post: abdlwafitahiri
  "I'm Feeling Lucky" script problem (again) tab_lo_lo 7 7,844 Jul-23-2019, 11:26 PM
Last Post: snippsat
  Need Help with Simple Text Reformatting Problem MattTuck 5 3,784 Aug-14-2017, 10:07 PM
Last Post: MattTuck

Forum Jump:

User Panel Messages

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