【问题标题】:I'm trying to scrape Startup-india website我正在尝试抓取 Startup-india 网站
【发布时间】:2020-01-16 07:49:39
【问题描述】:

我知道这不是错误,但我不明白如何抓取印度初创公司网站,我正在尝试点击印度初创公司提供的一些网站,但我无法点击它们,因为 scrapy 无法点击网站而我所掌握的任何信息都只能通过点击该链接来获取。

import scrapy
from selenium import webdriver
import os

class ProductSpider(scrapy.Spider):
    name = "product_spider"
    allowed_domains = ['https://www.startupindia.gov.in/']
    start_urls = ['https://www.startupindia.gov.in/content/sih/en/search.html?industries=sih:industry/advertising&states=sih:location/india/andhra-pradesh&stages=Prototype&roles=Startup&page=0']

    def __init__(self):
        cwd = os.getcwd()
        self.driver = webdriver.Chrome("C:/Users/RAJ/PycharmProjects/WebCrawler/WebCrawler/WebCrawler/spiders/chromedriver.exe")
        self.profile = []

    def parse(self, response):
        self.driver.get(response.url)

        while True:
            next = self.driver.find_element_by_xpath('//*[@id="persona-results"]/div[1]/div/a/div[1]')

            try:
                next.click()

                # get the data and write it to scrapy items
            except:
                break

        self.driver.close()

顺便说一句,我的最终目标是获取所有个人资料详细信息,但我不知道怎么做 (PS:这是我第一次做网页抓取)

【问题讨论】:

  • 如果只向我显示代码,我想点击网站会很棒。
  • 您不能通过 Scrapy 本身单击,而是可以通过某种方式获取 HREF。将链接和您的尝试放在您的问题中。
  • 不过我正在使用硒蜘蛛。

标签: python-3.x web-scraping scrapy


【解决方案1】:

这听起来类似于下面的 Scrapy 文档教程。一般来说,您可以尝试参考#follow links to author pages ,右键单击并检查要“单击”的位置以获取所需网页上的css/xpath。

https://docs.scrapy.org/en/latest/intro/tutorial.html

或者,随时分享您所拥有的。 希望这会有所帮助!

import scrapy


    class AuthorSpider(scrapy.Spider):
        name = 'author'

        start_urls = ['http://quotes.toscrape.com/']

        def parse(self, response):
            # follow links to author pages
            for href in response.css('.author + a::attr(href)'):
                yield response.follow(href, self.parse_author)

            # follow pagination links
            for href in response.css('li.next a::attr(href)'):
                yield response.follow(href, self.parse)

        def parse_author(self, response):
            def extract_with_css(query):
                return response.css(query).get(default='').strip()

            yield {
                'name': extract_with_css('h3.author-title::text'),
                'birthdate': extract_with_css('.author-born-date::text'),
                'bio': extract_with_css('.author-description::text'),
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多