Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:48:59+00:00 2026-06-01T14:48:59+00:00

I’m trying to loop through a text file with a list of urls and

  • 0

I’m trying to loop through a text file with a list of urls and have my python script parse each of the urls in the file.

The code only processes the LAST line in the file, when it should process every line and append the results to the file.

I have no idea what to do, i appreciate your help.
Thanks!

import feedparser # pip install feedparser
from BeautifulSoup import BeautifulStoneSoup
from BeautifulSoup import BeautifulSoup
import re


urls = open("c:/a2.txt", "r") # file with rss urls

for lines in urls:

    d = feedparser.parse(lines) # feedparser is supposed to process every url in the file(urls)


statusupdate = d.entries[0].description


soup = BeautifulStoneSoup(statusupdate)

for e in d.entries:
    print(e.title)
    print(e.link)
    print(soup.find("img")["src"])
    print("\n") # 2 newlines


# writes title,link,image to a file and adds some characters
    f = open(r'c:\a.txt', 'a')
    f.writelines('"')
    f.writelines(e.title)
    f.writelines('"')
    f.writelines(",")
    f.writelines('"')
    f.writelines(e.link)
    f.writelines('"')
    f.writelines(",")
    f.writelines('"')
    f.writelines(soup.find("img")["src"])
    f.writelines('"')
    f.writelines(",")
    f.writelines("\n")
f.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:49:00+00:00Added an answer on June 1, 2026 at 2:49 pm
    for lines in urls:
        d = feedparser.parse(lines)
    

    This loop simply keeps going and it keeps reassigning something to the variable d. That means, when the loop is finished, d will have the values associated with the last line.

    If you wish to process every line, you need to do something with every value of d. For example, you could put every d.entries[0].description in a list and then iterate over that list to process it.


    urls = open("c:/a2.txt", "r") # file with rss urls
    results = []
    for lines in urls:
        results.append(feedparser.parse(lines))
    contents = []
    for r in results:
        statusupdate = r.entries[0].description
        soup = BeautifulStoneSoup(statusupdate)
        for e in r.entries:
            contents.append((e.title, e.link, soup.find("img")["src"]))
    with open(r'c:\a.txt', 'a') as f:
        for c in contents:
            f.writelines('"')
            f.writelines(c[0])
            f.writelines('"')
            f.writelines(",")
            f.writelines('"')
            f.writelines(c[1])
            f.writelines('"')
            f.writelines(",")
            f.writelines('"')
            f.writelines(c[2])
            f.writelines('"')
            f.writelines(",")
            f.writelines("\n")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a reasonable size flat file database of text documents mostly saved in
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.