在网页上从下拉列表(例如日期)中进行选择

2024-09-30 18:20:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我是scrapy和python的新手,我尝试从下面的起始url中获取数据。在

登录后,这是我的起始url--->

启动URL=[“http://www.flightstats.com/go/HistoricalFlightStatus/flightStatusByFlight.do?”]在

(a)从那里我需要与网页互动选择---按机场---

然后做---机场,日期,时间段选择---

我怎么能做到呢?我想循环所有的时间段和过去的日期。。在

  • 我已经用萤火虫看到的来源,我不能显示在这里,因为我没有足够的点,以张贴图像。。

  • 我看到一个帖子提到了使用碎片。。

(b)在选择之后,它将引导我进入一个页面,其中有指向最终页面的链接,其中包含我想要的信息。我如何填充这些链接并让scrapy查找每个链接以提取信息?在

-使用规则?我应该在哪里插入rules/linkextractor函数?在

我愿意尝试自己,希望能帮我找到可以指导我的帖子。。我是一个学生,我已经花了一个多星期在这上面。。我已经完成了scrapy教程,python教程,阅读了scrapy文档,并在stackoverflow中搜索了以前的文章,但是我没有找到涵盖这一点的帖子。在

万分感谢。在

到目前为止,我要登录的代码以及要通过xpath从最终目标站点获取的项:

`import scrapy

from tutorial.items import FlightItem
from scrapy.http import FormRequest

class flightSpider(scrapy.Spider):
    name = "flight"
    allowed_domains = ["flightstats.com"]
    login_page =     'https://www.flightstats.com/go/Login/login_input.do;jsessionid=0DD6083A334AADE3FD6923ACB8DDCAA2.web1:8009?'
    start_urls = [
    "http://www.flightstats.com/go/HistoricalFlightStatus/flightStatusByFlight.do?"]

def init_request(self):
    #"""This function is called before crawling starts."""
    return Request(url=self.login_page, callback=self.login)

def login(self, response):
 #"""Generate a login request."""
    return FormRequest.from_response(response,formdata= {'loginForm_email': 'marvxxxxxx@hotmail.com', 'password': 'xxxxxxxx'},callback=self.check_login_response)

def check_login_response(self, response):
        #"""Check the response returned by a login request to see if we aresuccessfully logged in."""
    if "Sign Out" in response.body:
        self.log("\n\n\nSuccessfully logged in. Let's start crawling!\n\n\n")
         # Now the crawling can begin..

    return self.initialized() # ****THIS LINE FIXED THE LAST PROBLEM*****

    else:
            self.log("\n\n\nFailed, Bad times :(\n\n\n")
         # Something went wrong, we couldn't log in, so nothing happens.

def parse(self, response):
    for sel in response.xpath('/html/body/div[2]/div[2]/div'):
        item = flightstatsItem()
        item['flight_number'] = sel.xpath('/div[1]/div[1]/h2').extract()
        item['aircraft_make'] = sel.xpath('/div[4]/div[2]/div[2]/div[2]').extract()
        item['dep_date'] = sel.xpath('/div[2]/div[1]/div').extract()
        item['dep_airport'] = sel.xpath('/div[1]/div[2]/div[2]/div[1]').extract()
        item['arr_airport'] = sel.xpath('/div[1]/div[2]/div[2]/div[2]').extract()
        item['dep_gate_scheduled'] = sel.xpath('/div[2]/div[2]/div[1]/div[2]/div[2]').extract()
        item['dep_gate_actual'] = sel.xpath('/div[2]/div[2]/div[1]/div[3]/div[2]').extract()
        item['dep_runway_actual'] = sel.xpath('/div[2]/div[2]/div[2]/div[3]/div[2]').extract()
        item['dep_terminal'] = sel.xpath('/div[2]/div[2]/div[3]/div[2]/div[1]').extract()
        item['dep_gate'] = sel.xpath('/div[2]/div[2]/div[3]/div[2]/div[2]').extract()
        item['arr_gate_scheduled'] = sel.xpath('/div[3]/div[2]/div[1]/div[2]/div[2]').extract()
        item['arr_gate_actual'] = sel.xpath('/div[3]/div[2]/div[1]/div[3]/div[2]').extract()
        item['arr_terminal'] = sel.xpath('/div[3]/div[2]/div[3]/div[2]/div[1]').extract()
        item['arr_gate'] = sel.xpath('/div[3]/div[2]/div[3]/div[2]/div[2]').extract()

        yield item`

Tags: inselfdivcomresponseloginextractitem