Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

python - Scrapy : How to pass list of arguments through command prompt to spider?

Creating a scraper for fantasy team. Looking for a way to pass a list of the players names as arguments, and then for each player_name in player_list run the parsing code.

I currently have something like this

class statsspider(BaseSpider):
name = 'statsspider'

def __init__ (self, domain=None, player_list=""):
    self.allowed_domains = ['sports.yahoo.com']
    self.start_urls = [
        'http://sports.yahoo.com/nba/players',
    ]
    self.player_list= "%s" % player_list


def parse(self, response):
    example code
    yield request

I'm assuming entering a list of arguments is the same as just one argument through the command line so I enter something like this:

scrapy crawl statsspider -a player_list=['xyz','abc']

Problem 2!

Solved the first issue by inputting a comma delimited list of arguments like so

scrapy crawl statsspider -a player_list="abc def,ghi jkl"

I now want to go through each "name" (i.e. 'abc def') to find the first initial of their last name (in this case 'd').

I use the code

array = []
for player_name in self.player_list:
    array.append(player_name)
print array

And I end up with the result [["'",'a','b','c',... etc]] Why does python not assign player_name to each 'name' (e.g. 'abc def' and 'ghi jkl')? can someone explain this logic to me, and I will probably understand the right way to do it afterwards!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Shell arguments are string-based. You need to parse arg in your code.

command line:

scrapy crawl statsspider -a player_list=xyz,abc

python code:

self.player_list = player_list.split(',')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...