title: Python write web scrape data to csv
body: <p>I am still extremely new to Python, and I am working on an assignment for my school.</p>
  
  <p>I need to write code to pull all of the html from a website then save it to a <code>csv</code> file. </p>
  
  <p>I believe I somehow need to turn the links into a list and then write the list, but I'm unsure how to do that.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>import bs4
  import requests
  from bs4 import BeautifulSoup, SoupStrainer
  import csv
  
  search_link = "https://www.census.gov/programs-surveys/popest.html"
  r = requests.get(search_link)
  raw_html = r.text
  soup = BeautifulSoup(raw_html, 'html.parser')
  all_links = soup.find_all("a")
  
  rem_dup = set()
  for link in all_links:
      hrefs = str(link.get("href"))
      if hrefs.startswith('#http'):
          rem_dup.add(hrefs[1:])
      elif hrefs.endswith('.gov'):
          rem_dup.add(hrefs + '/')
      elif hrefs.startswith('/'):
          rem_dup.add('https://www.census.gov' + hrefs)
      else:
          rem_dup.add(hrefs)
  
  filename = "Page_Links.csv"
  f = open(filename, "w+")
  f.write("LINKS\n")
  f.write(all_links)
  f.close()
  </code></pre>