【问题标题】:Crawling: Extract all text and links(href and ng-href) from AngularJs website and Crawl抓取:从 AngularJs 网站和抓取中提取所有文本和链接(href 和 ng-href)
【发布时间】:2015-06-26 16:35:10
【问题描述】:

在尝试使用单点登录爬取有角度的 Js 页面进行了很多努力之后,我提出了这段代码。这段代码运行良好,登录会打开所需的页面并将其废弃,但我没有得到 angular 加载的网站中存在的所有链接和文本。我的 xpath 似乎是正确的。

此外,它不会抓取正在提取的链接。我需要更改我的代码以提取网站和后续网页中存在的所有文本?

import scrapy
from scrapy import signals
from scrapy.http import TextResponse 
from scrapy.xlib.pydispatch import dispatcher

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from ps_crawler.items import PsCrawlerItem
import time
from selenium.webdriver.common.keys import Keys

class SISSpider(scrapy.Spider):
    name = "SIS"
    allowed_domains = ["domain.com"]
    start_urls = ["https://domain.com/login?"]

    def __init__(self):
        self.driver = webdriver.Chrome()
        dispatcher.connect(self.spider_closed, signals.spider_closed)

    def spider_closed(self, spider):
        self.driver.close()

    def parse(self, response):
        # selenium part of the job
        self.driver.get("https://domain.com/login?")
        time.sleep(5)
        self.driver.find_element_by_xpath('//*[@id="Login"]/div[2]/div[1]/div[2]/form/div[1]/input').send_keys("ssasdad")
        self.driver.find_element_by_xpath('//*[@id="Login"]/div[2]/div[1]/div[2]/form/div[2]/input').send_keys("")
        #self.driver.find_element_by_xpath('//*[@id="login"]').click()

        more_btn = WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.XPATH, '//*[@id="login"]'))
                )
        time.sleep(5)
        more_btn.click()
        time.sleep(5)
        self.driver.execute_script("window.open('https://domain.com/#/admin','_blank');");
        time.sleep(10)
        window_now = self.driver.window_handles[1]
        self.driver.switch_to_window(window_now)
          ## stop when we reach the desired page
            #if self.driver.current_url.endswith('page=20'):
            #   break

         #now scrapy should do the job
        time.sleep(10)
        response = TextResponse(url=self.driver.current_url, body=self.driver.page_source, encoding='utf-8')
        time.sleep(10)
        for post in response.xpath('//div'):
            item = PsCrawlerItem()
            print post.xpath('a/span/text()').extract(), post.xpath('a/@href').extract(),  post.xpath('a/@ng-href').extract()

【问题讨论】:

  • 你知道页面上会有多少个链接吗?
  • 没有。我只是提取所有存在的东西。但是当我检查元素时,我发现大量的文本和链接被遗漏了。
  • 在获取page_source 并将其传递给 Scrapy 之前,您很可能需要等待。但是,问题是 - 等待什么?是否有任何迹象表明页面已完成加载?
  • 在传递页面源之前和 chrome 打开窗口时,我已经给出了 10 的睡眠时间。页面已完全加载。

标签: angularjs selenium web-scraping web-crawler scrapy


【解决方案1】:

您只需要按如下方式稍微调整一下您的 xpath。希望这能解决问题。

for post in response.xpath('//body'):
    print post.xpath('//text()').extract(), post.xpath('//a//@href').extract()

【讨论】:

    猜你喜欢
    • 2017-05-29
    • 2015-03-06
    • 2012-02-18
    • 2021-08-17
    • 2015-10-11
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多