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

  • SEARCH
  • Home
  • 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 7869921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:27:55+00:00 2026-06-03T01:27:55+00:00

I am trying to scrape table data into a CSV file. Unfortunately, I’ve hit

  • 0

I am trying to scrape table data into a CSV file. Unfortunately, I’ve hit a road block and the following code simply repeats the TD from the first TR for all subsequent TRs.

import urllib.request
from bs4 import BeautifulSoup

f = open('out.txt','w')

url = "http://www.international.gc.ca/about-a_propos/atip-aiprp/reports-rapports/2012/02-atip_aiprp.aspx"
page = urllib.request.urlopen(url)

soup = BeautifulSoup(page)

soup.unicode

table1 = soup.find("table", border=1)
table2 = soup.find('tbody')
table3 = soup.find_all('tr')

for td in table3:
    rn = soup.find_all("td")[0].get_text()
    sr = soup.find_all("td")[1].get_text()
    d = soup.find_all("td")[2].get_text()
    n = soup.find_all("td")[3].get_text()

    print(rn + "," + sr + "," + d + ",", file=f)

This is my first ever Python script so any help would be appreciated! I have looked over other question answers but cannot figure out what I am doing wrong here.

  • 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-03T01:27:57+00:00Added an answer on June 3, 2026 at 1:27 am

    You’re starting at the top level of your document each time you use find() or find_all(), so when you ask for, for example, all the “td”` tags you’re getting all the “td” tags in the document, not just those in the table and row you have searched for. You might as well not search for those because they’re not being used the way your code is written.

    I think you want to do something like this:

    table1 = soup.find("table", border=1)
    table2 = table1.find('tbody')
    table3 = table2.find_all('tr')
    

    Or, you know, something more like this, with more descriptive variable names to boot:

    rows = soup.find("table", border=1).find("tbody").find_all("tr")
    
    for row in rows:
        cells = row.find_all("td")
        rn = cells[0].get_text()
        # and so on
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to scrape some data from a page with a table based
I am trying to scrape an html table and save its data in a
Using the following code I am trying to scrape a call log from our
A page I am trying to scrape into a CSV database/Ruby array lists 470
Trying to scrape images and data from a website using Xpath but keeps showing
I'm having difficulties scraping dynamically generated table in ASPX. Trying to scrape the gas
I am learning Python - Beautiful Soup by trying to scrape data. I have
Hey can someone help with the following? I'm trying to scrape a site that
I'm trying to scrape some data off of the FEC.gov website using python for
I'm trying to scrape data from an external JSON feed and store it in

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.