Untitled
unknown
plain_text
a year ago
12 kB
5
Indexable
import scrapy from time import sleep from scrapy import Selector from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support.ui import WebDriverWait from ..items import DefaultRdcItem class TestRUSite(scrapy.Spider): name = "ru_federation_site" schedule = "" custom_settings = { "LOG_LEVEL": "INFO", } version = 1 start_urls = ["https://www.google.com/"] urls = [ "/ru/maps/ab/", "/ru/maps/au/", "/ru/maps/at/", "/ru/maps/az/", "/ru/maps/al/", "/ru/maps/dz/", "/ru/maps/ao/", "/ru/maps/ad/", "/ru/maps/ag/", "/ru/maps/ar/", "/ru/maps/am/", "/ru/maps/af/", "/ru/maps/bs/", "/ru/maps/bd/", "/ru/maps/bb/", "/ru/maps/bh/", "/ru/maps/bz/", "/ru/maps/by/", "/ru/maps/be/", "/ru/maps/bj/", "/ru/maps/bg/", "/ru/maps/bo/", "/ru/maps/ba/", "/ru/maps/bw/", "/ru/maps/br/", "/ru/maps/bn/", "/ru/maps/bf/", "/ru/maps/bi/", "/ru/maps/bt/", "/ru/maps/vu/", "/ru/maps/va/", "/ru/maps/gb/", "/ru/maps/hu/", "/ru/maps/ve/", "/ru/maps/tl/", "/ru/maps/vn/", "/ru/maps/ga/", "/ru/maps/ht/", "/ru/maps/gy/", "/ru/maps/gm/", "/ru/maps/gh/", "/ru/maps/gt/", "/ru/maps/gn/", "/ru/maps/gw/", "/ru/maps/de/", "/ru/maps/hn/", "/ru/maps/gd/", "/ru/maps/gr/", "/ru/maps/ge/", "/ru/maps/dk/", "/ru/maps/cd/", "/ru/maps/dj/", "/ru/maps/dm/", "/ru/maps/do/", "/ru/maps/eg/", "/ru/maps/zm/", "/ru/maps/zw/", "/ru/maps/il/", "/ru/maps/in/", "/ru/maps/id/", "/ru/maps/jo/", "/ru/maps/iq/", "/ru/maps/ir/", "/ru/maps/ie/", "/ru/maps/is/", "/ru/maps/es/", "/ru/maps/it/", "/ru/maps/ye/", "/ru/maps/kp/", "/ru/maps/cv/", "/ru/maps/kz/", "/ru/maps/kh/", "/ru/maps/cm/", "/ru/maps/ca/", "/ru/maps/qa/", "/ru/maps/ke/", "/ru/maps/cy/", "/ru/maps/kg/", "/ru/maps/ki/", "/ru/maps/cn/", "/ru/maps/co/", "/ru/maps/km/", "/ru/maps/cg/", "/ru/maps/sz/", "/ru/maps/cr/", "/ru/maps/ci/", "/ru/maps/cu/", "/ru/maps/kw/", "/ru/maps/la/", "/ru/maps/lv/", "/ru/maps/ls/", "/ru/maps/lr/", "/ru/maps/lb/", "/ru/maps/ly/", "/ru/maps/lt/", "/ru/maps/li/", "/ru/maps/lnr/", "/ru/maps/lu/", "/ru/maps/mu/", "/ru/maps/mr/", "/ru/maps/mg/", "/ru/maps/mw/", "/ru/maps/my/", "/ru/maps/ml/", "/ru/maps/mv/", "/ru/maps/mt/", "/ru/maps/ma/", "/ru/maps/mh/", "/ru/maps/mx/", "/ru/maps/fm/", "/ru/maps/mz/", "/ru/maps/md/", "/ru/maps/mc/", "/ru/maps/mn/", "/ru/maps/mm/", "/ru/maps/na/", "/ru/maps/nr/", "/ru/maps/np/", "/ru/maps/ne/", "/ru/maps/ng/", "/ru/maps/nl/", "/ru/maps/ni/", "/ru/maps/nz/", "/ru/maps/no/", "/ru/maps/ae/", "/ru/maps/om/", "/ru/maps/pk/", "/ru/maps/pw/", "/ru/maps/ps/", "/ru/maps/pa/", "/ru/maps/pg/", "/ru/maps/py/", "/ru/maps/pe/", "/ru/maps/pl/", "/ru/maps/pt/", "/ru/maps/kr/", # "/ru/maps/ru/", # delete "/ru/maps/rw/", "/ru/maps/ro/", "/ru/maps/us/", "/ru/maps/sv/", "/ru/maps/ws/", "/ru/maps/sm/", "/ru/maps/st/", "/ru/maps/sa/", "/ru/maps/mk/", "/ru/maps/sc/", "/ru/maps/sn/", "/ru/maps/vc/", "/ru/maps/kn/", "/ru/maps/lc/", "/ru/maps/rs/", "/ru/maps/sg/", "/ru/maps/sy/", "/ru/maps/sk/", "/ru/maps/si/", "/ru/maps/SB/", "/ru/maps/so/", "/ru/maps/sd/", "/ru/maps/sr/", "/ru/maps/sl/", "/ru/maps/tj/", "/ru/maps/th/", "/ru/maps/tz/", "/ru/maps/tg/", "/ru/maps/to/", "/ru/maps/tt/", "/ru/maps/tv/", "/ru/maps/tn/", "/ru/maps/tm/", "/ru/maps/tr/", "/ru/maps/ug/", "/ru/maps/uz/", "/ru/maps/ua/", "/ru/maps/uy/", "/ru/maps/fj/", "/ru/maps/ph/", "/ru/maps/fi/", "/ru/maps/fr/", "/ru/maps/hr/", "/ru/maps/cf/", "/ru/maps/td/", "/ru/maps/me/", "/ru/maps/cz/", "/ru/maps/cl/", "/ru/maps/ch/", "/ru/maps/se/", "/ru/maps/lk/", "/ru/maps/ec/", "/ru/maps/gq/", "/ru/maps/er/", "/ru/maps/ee/", "/ru/maps/et/", "/ru/maps/za/", "/ru/maps/os/", "/ru/maps/ss/", "/ru/maps/jm/", "/ru/maps/jp/", "/ru/maps/ru/ru-alt/", "/ru/maps/ru/ru-amu/", "/ru/maps/ru/ru-ark/", "/ru/maps/ru/ru-ast/", "/ru/maps/ru/ru-bel/", "/ru/maps/ru/ru-bry/", "/ru/maps/ru/ru-vla/", "/ru/maps/ru/ru-vgg/", "/ru/maps/ru/ru-vlg/", "/ru/maps/ru/ru-vor/", "/ru/maps/ru/ru-dnr/", "/ru/maps/ru/ru-yev/", "/ru/maps/ru/ru-zab/", "/ru/maps/ru/ru-zap/", "/ru/maps/ru/ru-iva/", "/ru/maps/ru/ru-irk/", "/ru/maps/ru/ru-kb/", "/ru/maps/ru/ru-kgd/", "/ru/maps/ru/ru-klu/", "/ru/maps/ru/ru-kam/", "/ru/maps/ru/ru-kc/", "/ru/maps/ru/ru-kem/", "/ru/maps/ru/ru-kir/", "/ru/maps/ru/ru-kos/", "/ru/maps/ru/ru-kda/", "/ru/maps/ru/ru-kya/", "/ru/maps/ru/ru-kgn/", "/ru/maps/ru/ru-krs/", "/ru/maps/ru/ru-len/", "/ru/maps/ru/ru-lip/", "/ru/maps/ru/ru-lnr/", "/ru/maps/ru/ru-mag/", "/ru/maps/ru/ru-mow/", "/ru/maps/ru/ru-mos/", "/ru/maps/ru/ru-mur/", "/ru/maps/ru/ru-nen/", "/ru/maps/ru/ru-niz/", "/ru/maps/ru/ru-ngr/", "/ru/maps/ru/ru-nvs/", "/ru/maps/ru/ru-oms/", "/ru/maps/ru/ru-ore/", "/ru/maps/ru/ru-orl/", "/ru/maps/ru/ru-pnz/", "/ru/maps/ru/ru-per/", "/ru/maps/ru/ru-pri/", "/ru/maps/ru/ru-psk/", "/ru/maps/ru/ru-ad/", "/ru/maps/ru/ru-al/", "/ru/maps/ru/ru-ba/", "/ru/maps/ru/ru-bu/", "/ru/maps/ru/ru-da/", "/ru/maps/ru/ru-in/", "/ru/maps/ru/ru-kl/", "/ru/maps/ru/ru-kr/", "/ru/maps/ru/ru-ko/", "/ru/maps/ru/ru-krm/", "/ru/maps/ru/ru-me/", "/ru/maps/ru/ru-mo/", "/ru/maps/ru/ru-sa/", "/ru/maps/ru/ru-se/", "/ru/maps/ru/ru-ta/", "/ru/maps/ru/ru-ty/", "/ru/maps/ru/ru-kk/", "/ru/maps/ru/ru-ros/", "/ru/maps/ru/ru-rya/", "/ru/maps/ru/ru-sam/", "/ru/maps/ru/ru-spe/", "/ru/maps/ru/ru-sar/", "/ru/maps/ru/ru-sak/", "/ru/maps/ru/ru-sve/", "/ru/maps/ru/ru-sev/", "/ru/maps/ru/ru-smo/", "/ru/maps/ru/ru-sta/", "/ru/maps/ru/ru-tam/", "/ru/maps/ru/ru-tve/", "/ru/maps/ru/ru-tom/", "/ru/maps/ru/ru-tul/", "/ru/maps/ru/ru-tyu/", "/ru/maps/ru/ru-ud/", "/ru/maps/ru/ru-uly/", "/ru/maps/ru/ru-kha/", "/ru/maps/ru/ru-khm/", "/ru/maps/ru/ru-khe/", "/ru/maps/ru/ru-che/", "/ru/maps/ru/ru-ce/", "/ru/maps/ru/ru-cu/", "/ru/maps/ru/ru-chu/", "/ru/maps/ru/ru-yan/", "/ru/maps/ru/ru-yar/", ] def __init__(self): user_agent = ( "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like" " Gecko) Chrome/110.0.0.0 Safari/537.36" ) options = webdriver.ChromeOptions() options.add_argument("disable-infobars") options.add_argument(f"user-agent={user_agent}") options.add_experimental_option( "excludeSwitches", ["enable-logging", "enable-automation"] ) options.add_experimental_option("useAutomationExtension", False) options.add_argument("--no-sandbox") options.add_argument("--disable-blink-features=AutomationControlled") self.driver = webdriver.Chrome( options=options, ) self.driver.maximize_window() self.driver.execute_script( "Object.defineProperty(navigator, 'webdriver', {get: () =>" " undefined})" ) def parse(self, response): for country_url in self.urls: start_url = f"https://www.mid.ru{country_url}" self.driver.get(start_url) self.driver.delete_all_cookies() if "https://www.mid.ru/ru/maps/ru/" in start_url: self.web_driver_wait_ru_territory(True) else: self.web_driver_wait_ru_territory() try: max_page_num = self.driver.find_element( By.XPATH, "//div[@class='pager__counter light-theme simple-pagination']/ul/li[last()-1]/a", ).text print(start_url, max_page_num) except: print("Only one page:", start_url) max_page_num = 1 if max_page_num: range_num = int(max_page_num) for num in range(1, range_num + 1): sleep(2) self.driver.get(f"{start_url}?PAGEN_1={num}") self.driver.delete_all_cookies() if "https://www.mid.ru/ru/maps/ru/" in start_url: self.web_driver_wait_ru_territory(True) self.driver.delete_all_cookies() select_response = Selector( text=self.driver.page_source ) yield from self.parse_response( select_response, isruterritory=True ) else: self.web_driver_wait_ru_territory() self.driver.delete_all_cookies() select_response = Selector( text=self.driver.page_source ) yield from self.parse_response( select_response, isruterritory=False ) self.driver.close() self.driver.quit() def parse_response(self, page_response, isruterritory=False): if isruterritory: news = page_response.xpath( "//ul[@class='announce main_page_news_list']/li" ) else: news = page_response.xpath( "//ul[@class='announce announce_articles']/li" ) for row in news: item = DefaultRdcItem() slug = row.xpath("./a/@href").get() url = f"https://mid.ru/{slug}" text = row.xpath("./a/text()").get().strip() if "заявление мид россии" in text.lower(): item["name"] = text item["url6"] = url print(item) yield item def web_driver_wait_ru_territory(self, dif_wait: bool = False): if dif_wait: WebDriverWait(self.driver, 100).until( ec.presence_of_element_located( ( By.XPATH, "//ul[@class='announce main_page_news_list']", ) ) ) else: WebDriverWait(self.driver, 100).until( ec.presence_of_element_located( ( By.XPATH, "//ul[@class='announce announce_articles']", ) ) )
Editor is loading...
Leave a Comment