Cina
Cina

Reputation: 10199

Python script for searching on Facebook ads using Pipenv

I want to run a Python script on Facebook ads search, but I get stuck in the middle, hoping you can help me (please see below). For your information, I do have a Facebook API token, but it seems I have a problem running Pipenv.

https://github.com/pandringa/fb-ads-tool

I will appreciate any feedback you provide!

import csv
import re
import argparse
import dateparser
In [8]:

parser = argparse.ArgumentParser(prog='Aggregates search results by key')
parser.add_argument('file')
parser.add_argument('-k', '--key')
parser.add_argument('-o', '--out')
Out[8]:
_StoreAction(option_strings=['-o', '--out'], dest='out', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
In [9]:

args = parser.parse_args()
infile = csv.DictReader(open(args.file))
usage: Aggregates search results by key [-h] [-k KEY] [-o OUT] file
Aggregates search results by key: error: unrecognized arguments: -f
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2


c:\users\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py:3339: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
In [10]:

rowset = {}
real_rowset = set()
In [11]:

out = csv.DictWriter(open(args.out, 'w'), fieldnames=[*infile.fieldnames, 'ad_versions_count'])
out.writeheader()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-4d5c0f933f42> in <module>
----> 1 out = csv.DictWriter(open(args.out, 'w'), fieldnames=[*infile.fieldnames, 'ad_versions_count'])
      2 out.writeheader()

NameError: name 'args' is not defined

Upvotes: 0

Views: 117

Answers (1)

kareem_emad
kareem_emad

Reputation: 1183

The error you are facing here is not related to Pipenv, it's related to the flow of your code, you probably ran the cells using args before defining it

I prefer to ran code like this in a script which I did with yours

import argparse
import csv

parser = argparse.ArgumentParser(prog='Aggregates search results by key')
parser.add_argument('file')
parser.add_argument('-k', '--key')
parser.add_argument('-o', '--out')

args = parser.parse_args()
infile = csv.DictReader(open(args.file))


rowset = {}
real_rowset = set()

out = csv.DictWriter(open(args.out, 'w'), fieldnames=[*infile.fieldnames, 'ad_versions_count'])
out.writeheader()

Running it with the command

python your_code.py --k key --o ./file.csv  ./sample.csv

it actually works perfectly and yield an output csv with the same columns as the input one. if you want to work with something like Jupyter or ipython then you do not need to use arg parser as you are not running python files with arguments, they are just cells executed in certain sequence. I don't think that is your end goal here but that what your code do for now, hope this helps and ready to provide more support related to fbads sdk or pipenv if needed

Upvotes: 1

Related Questions