python-2.7 - 创建局部变量来表示 xpath tr[ ] 内的数字范围?

标签 python-2.7 xpath web-scraping

我正在尝试抓取类似 thisthis 的俄勒冈州教师执照信息(这是公开数据)。我的问题是,由于有数百名教师拥有不同数量的执照和学区限制,我用来获取数据的 html 标签编号随着我尚未明确编码的每个新组合而变化。

这是我的代码的一部分,用于抓取第一个链接的数据。

for t in range(0,1000): #Drawing from a txt file with web address ids

    address = 'http://www.tspc.oregon.gov/lookup_application/LDisplay_Individual.asp?id=' + lines[t]

    page = requests.get(address)

    tree = html.fromstring(page.text)

    if "District Restriction" in dist_rest_find5: 
            print "dist rest 5"

            #Put Teacher License info into lists
        if "License Type" in tree.xpath('//tr[18]//text()'):
            test1 = tree.xpath('//tr[19]//text()')
            test1 = ([s.strip('\r') for s in test1])
            test1 = ([s.strip(' ') for s in test1])
            test1 = filter(None, test1)
            ltest1.append(test1)
        else:
            ltest1.append('')

        if "License Type" in tree.xpath('//tr[26]//text()'):
            test2 = tree.xpath('//tr[27]//text()')
            test2 = ([s.strip('\r') for s in test2])
            test2 = ([s.strip(' ') for s in test2])
            test2 = filter(None, test2)
            ltest2.append(test2)
        else:
            ltest2.append('')

我意识到对每个新组合进行编码需要几周的时间,并且我已经想到了一个解决方案,但我不知道如何将其转换为 Python。

我希望 if "License Type"in tree.xpath('//tr[18]//text()') 中的数字是循环遍历所有内容的范围的 tr[] 标记,直到满足条件,将许可证类型附加到列表,然后移动到下一个 if "License Type"in tree.xpath('//tr[ 26]//text()') 语句。我不希望有重复项,因此从第二个语句中获取的内容不能与第一个语句重叠。在 Stata 中,我会创建一个 local 来代替数字,但我不知道在 Python 中是否会使用相同的想法。

我想要的输出示例。

enter image description here

如果我说得不清楚,请告诉我。

最佳答案

据我了解,您基本上想从每个教师的页面获取所有许可证。这里的想法是找到第一个单元格内具有 License Type 文本的行,然后获取第一个 following tr sibling该行的。

实现:

import requests
from lxml import html


url = "http://www.tspc.oregon.gov/lookup_application/LDisplay_Individual.asp?id=535454R3L38"
page = requests.get(url)

tree = html.fromstring(page.text)
for license_row in tree.xpath(".//tr[td[1] = 'License Type']/following-sibling::tr[1]"):
    license_data = license_row.xpath(".//td/text()")
    print(license_data)

打印:

['Initial II Teaching', '5/31/2015', '6/9/2018', 'Active']
['Initial II School Counselor', '6/10/2014', '6/9/2017', 'Active']
['Initial Administrator', '6/10/2014', '7/10/2016', 'Active']
['Initial I School Counselor', '6/10/2008', '6/9/2011', 'Expired']
['Conditional Permit', '10/3/2006', '10/2/2008', 'Expired']
['Initial School Counselor', '4/26/2005', '6/9/2008', 'Expired']
['Initial I Teaching', '6/13/2002', '6/9/2006', 'Expired']
['Conditional Permit', '12/21/2002', '12/20/2005', 'Expired']
['Conditional Permit', '3/1/2004', '12/20/2005', 'Expired']
['Conditional Permit', '9/1/2004', '4/25/2005', 'Expired']
['Transitional Teaching', '7/24/2001', '7/24/2004', 'Expired']
['Expedited Service', '7/24/2001', '7/24/2004', 'Expired']
['Restricted Transitional Teaching', '7/24/2001', '7/24/2004', 'Expired']

关于python-2.7 - 创建局部变量来表示 xpath tr[ ] 内的数字范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37219168/

相关文章:

css - 为什么google-chrome-devtools通过XPath识别的元素数量少于通过CssSelector识别的元素数量

python - 该 Selenium 元素的正确元素类型是什么?

python - 删除python字典中的引号

java - 带有查找元素命令的字符串变量 Java

python - 检查嵌套属性是否存在

python - XPath:在一个表达式中匹配多个元素

javascript - 如何解决HTMLUnit中的 "JavaScriptException value = SyntaxError: with statements not allowed in strict mode"

javascript - 使用 PhantomJS 进行网页抓取

python - 需要一个测试用例,其中给定的最小硬币数量代码在 python 中失败了吗?

python - 使属性方法可调用