Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing infor from scraped files.
#1
I am trying to get one simple bit of data from several thousand scraped files.
I want to do this using concurrent futures, but am having a bit of an issue
I created a sample which contains just 10 files, for testing and it looks like this:
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
from pathlib import Path
from bs4 import BeautifulSoup
import os


class Scrape3:
    def __init__(self):
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        filepath = Path('./html')
        citylist = [
            ['Andover'],
            ['Berlin'],
            ['Brooklyn'],
            ['Burlington'], 
            ['Colchester'],
            ['Groton'],
            ['Hartland'],
            ['Kent'],
            ['Manchester'],
            ['Marlborough']
        ]

        for city in citylist:
            city.append(filepath / f'{city[0]}_page1.html')

        # for item in citylist:
        #     print(f'{item[0]}, {item[1].resolve()}')

        self.numpages = []
        # self.test_parse(citylist)
        self.get_numpages(citylist)

        print(f'numpages: {self.numpages}')

    def parse(self, city):
        with city[1].open('rb') as fp:
            page = fp.read()
        soup = BeautifulSoup(page, 'lxml')
        return [city[0], str(soup.find('span', {'class': "paginate-info"}).text.split()[2])]

    def test_parse(self, citylist):
        for city in citylist:
            print(self.parse(city))

    def get_numpages(self, citylist):
        ex = ThreadPoolExecutor(max_workers=10)

        for city in citylist:
            wait_for = [
                ex.submit(self.parse(city))
            ]

            for f in as_completed(wait_for):
                self.numpages.append(f.result)

if __name__ == '__main__':
    Scrape3()
It all appears to function properly until it comes to what's getting stored in self.numpages.
I expected a list of lists, each containing city name and number of pages, but what I get is:
Output:
numpages: [<bound method Future.result of <Future at 0x7f23423502e8 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f23413fe390 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f2340b930b8 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f2340320e80 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f23402fe278 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f2340bbbf60 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f234029d240 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f234022e198 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f234029dcc0 state=finished raised TypeError>>, <bound method Future.result of <Future at 0x7f234034f0f0 state=finished raised TypeError>>]
I am missing something. Don't know what's creating the TypeError. Anybody know what that might be?

I tested the return statement (by running test_parse) and it does what it's supposed to do:
[city[0], str(soup.find('span', {'class': "paginate-info"}).text.split()[2])]
Output:
['Andover', '18'] ['Berlin', '91'] ['Brooklyn', '76'] ['Burlington', '59'] ['Colchester', '77'] ['Groton', '92'] ['Hartland', '1'] ['Kent', '23'] ['Manchester', '278'] ['Marlborough', '39']
Quote
#2
Missing the () off f.result
buran likes this post
Quote
#3
I knew it was something stupid, Thanks!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Fetching and Parsing XML Data FalseFact 3 144 Apr-01-2019, 10:21 AM
Last Post: Larz60+
  Selenium Parsing (unable to Parse page after loading) oneclick 6 400 Jan-13-2019, 03:10 AM
Last Post: oneclick
  XML parsing from URL mightyn00b 5 905 Nov-22-2018, 02:59 AM
Last Post: Larz60+
  XML Parsing - Find a specific text (ElementTree) TeraX 3 384 Oct-09-2018, 09:06 AM
Last Post: TeraX
  XML parsing and generating HTML page Python 3.6 Madhuri 2 400 Aug-24-2018, 02:48 PM
Last Post: snippsat
  Problem parsing website html file thefpgarace 2 592 May-01-2018, 11:09 AM
Last Post: Standard_user
  parsing table ian 10 2,355 Apr-27-2018, 01:05 PM
Last Post: snippsat
  beautiful soup - parsing scraped code in a script lilbigwill99 2 481 Mar-09-2018, 04:10 PM
Last Post: lilbigwill99
  BeautifulSoup Parsing Error slinkplink 6 3,357 Feb-12-2018, 02:55 PM
Last Post: seco
  Need Tip On Cleaning My BS4 Scraped Data digitalmatic7 2 566 Jan-29-2018, 08:49 PM
Last Post: digitalmatic7

Forum Jump:


Users browsing this thread: 1 Guest(s)