Как получить доступ к данным из одной функции в другой функции - PullRequest
0 голосов
/ 30 мая 2019

У меня есть программа очистки веб-страниц, и в моем пауке мне нужен доступ к информации в моей функции process_csv, но мне нужно получить доступ к ней из моей функции start_requests. в моей функции process_csv у меня есть переменная "ip", которая берет ips из CSV-файла и добавляет http: // в начало и номер порта в конец, а затем функция связывает их с пользовательским агентом и URL-адресом, который требуется быть очищенным. В любом случае мне нужен доступ к этим ips в моей функции start_requests, поэтому мне нужно вызвать переменную "ip" из моей функции start_requests. Как бы я это сделал?

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   

1 Ответ

2 голосов
/ 30 мая 2019

В вашей функции process_csv вы добавляете список с именем data с помощью {"url": url, "ip": ip, "ua": useragent}. Таким образом, вы можете вызвать каждую из этих записей из функции, которая будет возвращать dict, а затем вы можете вызвать ip. Пример:

request_ip = process_csv(csv_file)[0]["ip"] - вернет ip в первой записи в списке. Вы можете запустить цикл, чтобы получить все IP-адреса и сохранить их в списке.

Надеюсь, это поможет.

...