Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How do I extract specific lines from HTML files before and after a word?
#1
I am trying to extract the 10 lines before and after the word "apple" from a directory (with subdirectories) full of HTML files. I want to print out the lines into a CSV file. Ideally, the CSV file will contain two variables: 1) the HTML filename and 2) the 10 lines before and after the word "apple".

I have done the following:

import glob
import collections
import itertools
import sys
import csv

for filepath in glob.glob('**/*.html', recursive=True):
    with open(filepath) as f:
        before = collections.deque(maxlen=10)
        for line in f:
            if 'apple' in line:
                sys.stdout.writelines(before)
                sys.stdout.write(line)
                sys.stdout.writelines(itertools.islice(f, 10))
            break
        results = before.append(line)
        print(results)
I am currently getting a bunch of rows that say "None" in my terminal when I print the results. What is the issue here?
Quote
#2
Why do you expect that "append" method returns a value?
https://docs.python.org/2/library/collec...que.append
Nothing about the value returned. In case if a function doesn't return a result python always returns None.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  HTML Styling Not Working yoitspython 1 109 Aug-13-2019, 06:26 AM
Last Post: fishhook
  spliting html code with br tag yokaso 11 263 Aug-07-2019, 03:18 PM
Last Post: snippsat
  How do I get rid of the HTML tags in my output? glittergirl 1 310 Aug-05-2019, 08:30 PM
Last Post: snippsat
  convert html table to json bhojendra 5 159 Jul-30-2019, 07:53 PM
Last Post: DeaD_EyE
  How to capture Single Column from Web Html Table? ahmedwaqas92 5 287 Jul-29-2019, 02:17 AM
Last Post: ahmedwaqas92
  Selenium fields containing a word graham23s 2 105 Jul-23-2019, 10:44 PM
Last Post: graham23s
  TypeError: missing 1 required positional argument (word counter Django app) Drone4four 2 196 Jul-11-2019, 09:34 PM
Last Post: Drone4four
  Getting a specific text inside an html with soup mathieugrimbert 9 366 Jul-10-2019, 12:40 PM
Last Post: mathieugrimbert
  How to use Python to extract data from Zoho Creator software on the web dan7055 2 233 Jul-05-2019, 05:11 PM
Last Post: DeaD_EyE
  Python/BeautiifulSoup. list of urls ->parse->extract data to csv. getting ERROR IanTheLMT 2 220 Jul-04-2019, 02:31 AM
Last Post: IanTheLMT

Forum Jump:


Users browsing this thread: 1 Guest(s)