Python 如何从一个函数访问另一个函数中的数据

Python 如何从一个函数访问另一个函数中的数据,python,scrapy,splash-screen,scrapy-splash,Python,Scrapy,Splash Screen,Scrapy Splash,我有一个网页抓取程序,在我的spider中,我需要访问process_csv函数中的信息,但我需要从start_requests函数访问它。在我的process_csv函数中,我有一个变量“ip”,它从csv文件中获取ip,并将http://添加到开头,将端口号添加到结尾,然后该函数继续将它们与需要删除的用户代理和URL配对。无论如何,我需要在start_requests函数中访问这些ip,因此我需要从start_requests函数调用变量“ip”。我该怎么做 SplashSpider.py

我有一个网页抓取程序,在我的spider中,我需要访问process_csv函数中的信息,但我需要从start_requests函数访问它。在我的process_csv函数中,我有一个变量“ip”,它从csv文件中获取ip,并将http://添加到开头,将端口号添加到结尾,然后该函数继续将它们与需要删除的用户代理和URL配对。无论如何,我需要在start_requests函数中访问这些ip,因此我需要从start_requests函数调用变量“ip”。我该怎么做

SplashSpider.py

import csv
from scrapy.spiders import Spider
from scrapy_splash import SplashRequest
from ..items import GameItem

# process the csv file so the url + ip address + useragent pairs are the same as defined in the file # returns a list of dictionaries, example:
# [ {'url': 'http://www.starcitygames.com/catalog/category/Rivals%20of%20Ixalan',
#    'ip': 'http://204.152.114.244:8050',
#    'ua': "Mozilla/5.0 (BlackBerry; U; BlackBerry 9320; en-GB) AppleWebKit/534.11"},
#    ...
# ]
def process_csv(csv_file):
    data = []
    reader = csv.reader(csv_file)
    next(reader)
    for fields in reader:
        if fields[0] != "":
            url = fields[0]
        else:
            continue # skip the whole row if the url column is empty
        if fields[1] != "":
            ip = "http://" + fields[1] + ":8050" # adding http and port because this is the needed scheme
        if fields[2] != "":
            useragent = fields[2]
        data.append({"url": url, "ip": ip, "ua": useragent})
    return data


class MySpider(Spider):
    name = 'splash_spider'  # Name of Spider

    # notice that we don't need to define start_urls
    # just make sure to get all the urls you want to scrape inside start_requests function

    # getting all the url + ip address + useragent pairs then request them
    def start_requests(self):

        # get the file path of the csv file that contains the pairs from the settings.py
        with open(self.settings["PROXY_CSV_FILE"], mode="r") as csv_file:
            # requests is a list of dictionaries like this -> {url: str, ua: str, ip: str}
            requests = process_csv(csv_file)

        for req in requests:
            # no need to create custom middlewares
            # just pass useragent using the headers param, and pass proxy using the meta param
            yield SplashRequest(url=req["url"], callback=self.parse, args={"wait": 3},
                                splash_url = ip,
                                headers={"User-Agent": req["ua"]},
                                meta={
                                    "proxy": (req["ip"]),
                                  })

    # Scraping
#    def parse(self, response):
#        item = GameItem()
#        for game in response.css("tr"):
#            # Card Name
#            item["card_name"] = game.css("a.card_popup::text").extract_first()
#            yield item   

process\u csv
函数中,您将添加一个名为
data
的列表,其中包含
{“url”:url,“ip”:ip,“ua”:useragent}
。因此,您可以从函数中调用这些记录中的每一个,这将返回一个dict,然后您可以调用ip。例如:

request\u ip=process\u csv(csv\u文件)[0][“ip”]
-将返回列表中第一条记录中的ip。您可以运行一个循环来获取所有ip并将它们存储在一个列表中


希望这有帮助

在您的
process\u csv
函数中,您将添加一个名为
data
的列表,其中包含
{“url”:url,“ip”:ip,“ua”:useragent}
。因此,您可以从函数中调用这些记录中的每一个,这将返回一个dict,然后您可以调用ip。例如:

request\u ip=process\u csv(csv\u文件)[0][“ip”]
-将返回列表中第一条记录中的ip。您可以运行一个循环来获取所有ip并将它们存储在一个列表中


希望这有帮助

process\u csv
函数的返回已经在
start\u requests
方法中。在这一行
requests=process\u csv(csv\u文件)
中,您将其返回存储在此变量上。您可以使用
for
中的
req[“ip”]
访问ip,并且您已经在这样做了。
process\u csv
函数的返回已经在
start\u requests
方法中。在这一行
requests=process\u csv(csv\u文件)
中,您将其返回存储在此变量上。您可以,并且您已经在这样做了,在您的
for
中使用
req[“ip”]
访问ip。