【问题标题】:Python Scrappy Xpath - can't extract selected data from tablePython Scrapy Xpath - 无法从表中提取选定的数据
【发布时间】:2017-11-04 11:33:30
【问题描述】:

我正在尝试提取按国家分组的附表的所有数据,其中包含国家名称, http://applications.slbfe.lk/jobbank/jsearchdisplay_an_m.asp?an=1712 我试图执行

response.xpath('//div').xpath('.//tr[@bgcolor="#CCCCCC"]/td/b/font/text()').extract()

在这里我选择国家名称,但我如何才能获得该国家名称下的所有数据,例如每个国家/地区的已批准空缺率

【问题讨论】:

标签: python xpath web-scraping beautifulsoup scrapy


【解决方案1】:

由于您尚未展示如何编写代码来解析表格数据,因此我在此处提供了一个演示,让您了解如何解析表格中的选择性数据。就像我在这里做的那样,在你的代码中抽动选择器:

from bs4 import BeautifulSoup
import requests

link = "http://applications.slbfe.lk/jobbank/jsearchdisplay_an_m.asp?an=1712"
res = requests.get(link).text
soup = BeautifulSoup(res,"lxml")
table = soup.select("table")[3]
for items in table.select('tr'):
    item_name = [' '.join(item.text.split()) for item in items.select('td')[:3]] #this is where you change the index which column to parse
    print(' '.join(item_name))

部分结果:

Details Approved Vacancies Available Vacancies
Kuwait
House Boy 10 10
House Boy 10 8
House Cook - Male 10 10
House Cook - Male 10 8

【讨论】:

    猜你喜欢
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2021-12-03
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    相关资源
    最近更新 更多