【问题标题】:python scrapy: scraping dynamic informationpython scrapy:抓取动态信息
【发布时间】:2014-07-27 15:04:08
【问题描述】:

我正在尝试从http://www.qchp.org.qa/en/Pages/searchpractitioners.aspx 中删除信息。我想做以下事情: - 从页面顶部的下拉菜单中选择“牙医” - 点击搜索 - 注意页面底部的信息使用javascript动态变化 - 单击从业者姓名的超链接,会出现一个弹出窗口 - 我想将所有这些信息保存在每个从业者的 json/csv 文件中 - 我还想要在页面底部链接的其他页面上的信息,这些信息会更改保存 div 中的信息。

我对scrapy很陌生,刚刚研究了硒,因为我在某个地方读到了你需要硒来获取动态信息

所以我在一个 scrapy 应用程序中使用 Selenium。不确定这是否正确。我不知道最好的方法是什么。到目前为止,我有以下代码。我收到了这个错误 sch_spider.py",

line 21, in DmozSpider
    all_options = element.find_elements_by_tag_name("option")
NameError: name 'element' is not defined

sch_spider.py

from scrapy.spider import Spider
from scrapy.selector import Selector
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from scrapytutorial.items import SchItem
from selenium.webdriver.support.ui import Select

class DmozSpider(Spider):
    name = "sch"

    driver = webdriver.Firefox()
    driver.get("http://www.qchp.org.qa/en/Pages/searchpractitioners.aspx")
    select = Select(driver.find_element_by_name('ctl00$m$g_28bc0e11_4b8f_421f_84b7_d671de504bc3$ctl00$drp_practitionerType'))
    all_options = element.find_elements_by_tag_name("option")

    for option in all_options:
        if option.get_attribute("value") == "4":  #Dentist
            option.click()
        ends
        break

    driver.find_element_by_name("ctl00$m$g_28bc0e11_4b8f_421f_84b7_d671de504bc3$ctl00$Searchbtn").click()


    def parse(self, response):

        all_docs = element.find_elements_by_tag_name("td")
        for name in all_docs:
            name.click()
            alert = driver.switch_to_alert()
            sel = Selector(response)
            ma = sel.xpath('//table')
            items = []
            for site in ma:
                item = SchItem()
                item['name'] = site.xpath("//span[@id='PractitionerDetails1_lbl_Name']/text()").extract()
                item['profession'] = site.xpath("//span[@id='PractitionerDetails1_lbl_Profession']/text()").extract()
                item['scope_of_practise'] = site.xpath("//span[@id='PractitionerDetails1_lbl_sop']/text()").extract()
                item['instituition'] = site.xpath("//span[@id='PractitionerDetails1_lbl_institution']/text()").extract()
                item['license'] = site.xpath("//span[@id='PractitionerDetails1_lbl_LicenceNo']/text()").extract()
                item['license_expiry_date'] = site.xpath("//span[@id='PractitionerDetails1_lbl_LicenceExpiry']/text()").extract()
                item['qualification'] = site.xpath("//span[@id='PractitionerDetails1_lbl_Qualification']/text()").extract()

                items.append(item)
            return items

items.py

from scrapy.item import Item, Field

class SchItem(Item):

    name = Field()
    profession = Field()
    scope_of_practise = Field()
    instituition = Field()
    license = Field()
    license_expiry_date = Field()
    qualification = Field()

【问题讨论】:

  • 我不是在寻找代码审查。我有一个错误,正在寻找解决方案。
  • 你应该向服务器发送一个 POST 请求。This answer here 应该是一个好的开始。

标签: javascript python selenium dynamic scrapy


【解决方案1】:

你不应该把下面代码中的 element.find_elements .. 改成 select.find_element..

  select = Select(driver.find_element_by_name('ctl00$m$g_28bc0e11_4b8f_421f_84b7_d671de504bc3$ctl00$drp_practitionerType'))
  all_options = element.find_elements_by_tag_name("option")

或者说不应该使用 select.options ?

【讨论】:

    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多