这是网站,我要刮取对应于类别
和子类别
的表数据。
https://www.adbc.gov.ae/bussinessactivityinfo/bussinessactivity.aspx?culture=en-us
我用Scrapy和硒来刮。 这是我的代码:
导入某些库:
import scrapy
from scrapy.selector import Selector
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.support.ui import Select
主代码:
class AdbcSpiderSelenium(scrapy.Spider):
name = 'adbc_selenium'
allowed_domains = ['https://www.adbc.gov.ae/BussinessActivityInfo/BussinessActivity.aspx?culture=en-US']
start_urls = [
'https://www.adbc.gov.ae/BussinessActivityInfo/BussinessActivity.aspx?culture=en-US'
]
def __init__(self):
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_path = 'D:\\work\\crawl data\\selenium_project\\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.get("https://www.adbc.gov.ae/BussinessActivityInfo/BussinessActivity.aspx?culture=en-US")
tab = Select(driver.find_element_by_id("ddlNatureId"))
tab.select_by_value('ADVOCATE OFFICES')
tab = Select(driver.find_element_by_id("ddlSubCategId"))
tab.select_by_value('Advertising Agent')
self.html = driver.page_source
driver.close()
def parse(self, response):
resp = Selector(text=self.html)
contents = resp.xpath('//*[@id="gvActivities"]/table//tr')
for content in contents:
yield {
'code': content.xpath('td[1]//text()').get(),
'name': content.xpath('td[2]//text()').get()
}
我的代码试图清除Business Category=ADVOCATE Offices
和Business SubCategory=Advertising Agent
中的表数据,只有上面的图像。
代码可以运行,但它不返回表的任何数据。 我的代码怎么了?
下面是要使用的正确xpath://table[@id=“GVActivities”]//TR