Python Forum
Scraping results won't save in csv file
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scraping results won't save in csv file
#1
Hi Everyone, I am very new to this but managed to last night get a succesful scrape using the code below. However, I must have an error somewhere in the code as while the code creates a csv file the results of the scrape don't end up in that file; they just stay in the terminal on my MAC. I am using python 3.6.2

Can anyone with more experience let me know which code I need to change to fix this?

from collections import deque
from urllib.parse import urljoin
import requests
import csv
b =open('bars1.csv','a')
a =csv.writer(b)
data = [['sales-info', 'email-business']]
a.writerows(data)
b.close()
from lxml import html

class YellowPage:
main_url = "https://www.yellowpages.com/search?search_terms=Bars&geo_location_terms=Miami%2C+FL"

def __init__(self):
self.links = [self.main_url]
self.storage =

def crawl(self):

for link in self.links :
self.get_link(link)

def get_link(self, link):

print('Scraping Now: ' + link)
url = "https://www.yellowpages.com"
response = requests.get(link)
tree = html.fromstring(response.text)

#scraping links of each bars
for items in tree.xpath("//div[@class='info']"):
link_page = items.xpath(".//a[@class='business-name'][not(@itemprop='name')]/@href")
for page in link_page:
if page and url + page not in self.links:
self.links += [url + page]

#parsing the links to the next page
next_page = tree.xpath("//div[@class='pagination']//li/a/@href")
for nepage in next_page:
if nepage and url + nepage not in self.links:
self.links += [url + nepage]

#going to the main page of each bars and harvest the record
for posts in tree.xpath("//*[@id='main-header']"):
name = posts.xpath(".//div[@class='sales-info']/h1/text()")[0] if posts.xpath(".//div[@class='sales-info']/h1/text()") else ""
email = posts.xpath(".//a[@class='email-business']/@href")[0] if posts.xpath(".//a[@class='email-business']/@href") else ""
records = name, email
self.storage.append(records)


def __str__(self):
return self.storage


crawler = YellowPage()
crawler.crawl()
for item in crawler.storage:
print(item)
Reply
#2
First, use python tags when posting code. See the BBCode link in my signature below for instructions.

Second, I don't see where you're writing anything but the column headers to the csv file. You need to write those rows as you wrote the column headers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks Craig for your prompt response and apologies for the lack of Python tags; I am all new to this. Ok will go and do some more research in to what code to use to write the rows to the csv file.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 316 Jan-24-2024, 06:28 PM
Last Post: frohr
  Updating sharepoint excel file odd results cubangt 1 825 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  how to save to multiple locations during save cubangt 1 544 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,207 Jul-07-2023, 05:44 PM
Last Post: flash77
  Save and Close Excel File avd88 0 3,023 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to send file to printer with no results. chob_thomas 2 3,369 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
  Save multiple Parts of Bytearray to File ? lastyle 1 941 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,370 Sep-27-2022, 01:38 PM
Last Post: buran
  How to format EasyOCR results in order to save to CSV? cubangt 0 1,254 Sep-02-2022, 02:06 PM
Last Post: cubangt
  [split] Results of this program in an excel file eisamabodian 1 1,575 Feb-11-2022, 03:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020