简体   繁体   中英

Error Web Scraping with ThreadPoolExecutor

I'm working on a simple web scraping program, but I can't even seem to download a simple set of pages and get their sizes.

Here is my code:

from concurrent.futures import ThreadPoolExecutor as Executor
urls = """reddit twitter tumblr instagram linkedin""".split()

def fetch(url):
    from urllib import request, error
    try:
        data = request.urlopen(url).read()
        return '{}: length {}'.format(url, len(data))
    except error.HTTPError as e:
        return '{}: {}'.format(url, e)

with Executor(max_workers=4) as exe:
    template = 'http://www.{}.com'
    jobs = [exe.submit(
        fetch, template.format(u)) for u in urls]
    results = [job.result() for job in jobs]

print('\n'.join(results))

In the command line I'm running

python scrape.py

but I'm getting the error

Traceback (most recent call last): File "scrape.py", line 1, in from concurrent.futures import ThreadPoolExecutor as Executor ImportError: No module named concurrent.futures

What do I need to do to surmount this error?

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