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
458 views
in Technique[技术] by (71.8m points)

python - Unable to scrape the Next page URLs using Selenium and scrapy

I am struggling to parse/scrape each page after clicking the Next button using Selenium. I am able to go to the second page, however, it fails after that. Not sure how to solve this, any suggestions? Here is the code:

class PropertyFoxSpider(scrapy.Spider):
    name = 'property_fox'
    start_urls = [
        'https://propertyfox.co.za/listing-search?currentpage=1&term_id=62515&keywords=Western+Cape&orderby=createddate:desc&status%5B%5D=Active'
    ]


    def __init__(self):
        #path to driver
        self.driver = webdriver.Chrome('path')


    def parse(self,response):
        self.driver.get(response.url)
        while True: 
            try: 
                elem = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.ID, "pagerNext")))
                elem.click()
                url = self.driver.current_url
                yield scrapy.Request(url=url, callback=self.parse_page, dont_filter=False)
            except TimeoutException:
                break



    def parse_page(self, response):
        #self.driver.get(response.url)
        for prop in response.css('div.property-item'):
            link = prop.css('a::attr(href)').get()
            banner = prop.css('div.property-figure-icon div::text').get()
            sold_tag = None
            if banner:
                banner = banner.strip()
                sold_tag = 'sold' if 'sold' in banner.lower() else None

            yield scrapy.Request(
                link,
                callback=self.parse_property,
                meta={'item': {
                    'agency': self.name,
                    'url': link,
                    'offering': 'buy',
                    'banners': banner,
                    'sold_tag':  sold_tag,
                }},
            )
    def parse_property(self, response):
        item = response.meta.get('item')
     ...

question from:https://stackoverflow.com/questions/65901666/unable-to-scrape-the-next-page-urls-using-selenium-and-scrapy

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

1 Reply

0 votes
by (71.8m points)

You can wait until URL changed and then scrape it

from selenium.webdriver.support.ui import WebDriverWait

url = self.driver.current_url
elem = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.ID, "pagerNext")))
elem.click()
WebDriverWait(self.driver, 10).until(lambda driver: self.driver.current_url != url)
url = self.driver.current_url

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

...