基于当前行的值获取下一行的值

2024-05-19 22:25:45 发布

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

设置

我需要获得this Wikipedia page上所有NUTS3区域的总体数据。你知道吗

我已经获得了每个NUTS3区域的所有url,并将让Selenium在它们上面循环,以获得每个区域在其页面上显示的人口数量。你知道吗

也就是说,对于每个区域,我需要在其infobox geography vcard元素中显示总体。例如,对于this region,总体将是591680。你知道吗


代码

在写循环之前,我想得到一个区域的人口

url = 'https://en.wikipedia.org/wiki/Arcadia'

browser.get(url)

vcard_element = browser.find_element_by_css_selector('#mw-content-text > div > table.infobox.geography.vcard').find_element_by_xpath('tbody')

for row in vcard_element.find_elements_by_xpath('tr'):

    try:
        if 'Population' in row.find_element_by_xpath('th').text:
            print(row.find_element_by_xpath('th').text)
    except Exception:
        pass

问题

代码起作用了。也就是说,它打印包含单词“Population”的行。你知道吗

问题:如何告诉Selenium获取下一行–包含实际人口数的行?你知道吗


Tags: text区域urlbyseleniumelementfindthis
2条回答

虽然您当然可以使用selenium来实现这一点,但我个人建议使用requests和lxml,因为它们的重量比selenium轻得多,而且也可以很好地完成工作。我发现以下方法适用于我测试的几个地区:

try:
    response = requests.get(url)

    infocard_rows = html.fromstring(response.content).xpath("//table[@class='infobox geography vcard']/tbody/tr")

except:
    print('Error retrieving information from ' + url)


try:
    population_row = 0
    for i in range(len(infocard_rows)):
        if infocard_rows[i].findtext('th') == 'Population':
            population_row = i+1
            break

    population = infocard_rows[population_row].findtext('td')

except:
    print('Unable to find population')

从本质上说html.fromstring().xpath()正在获取路径上infobox geography vcard表中的所有行。下一个try-catch只尝试定位其内部文本为thPopulation,然后从下一个td中提取文本(这是总体数)。你知道吗

希望这是有帮助的,即使它不是像你所要求的那样!如果您想重新创建浏览器行为或检查javascript元素,通常会使用Selenium。你当然也可以在这里用。你知道吗

使用./following::tr[1]./following-sibling::tr[1]

url = 'https://en.wikipedia.org/wiki/Arcadia'
browser=webdriver.Chrome()
browser.get(url)

vcard_element = browser.find_element_by_css_selector('#mw-content-text > div > table.infobox.geography.vcard').find_element_by_xpath('tbody')

for row in vcard_element.find_elements_by_xpath('tr'):

    try:
        if 'Population' in row.find_element_by_xpath('th').text:
            print(row.find_element_by_xpath('th').text)
            print(row.find_element_by_xpath('./following::tr[1]').text) #whole word
            print(row.find_element_by_xpath('./following::tr[1]/td').text) #Only number
    except Exception:
        pass

控制台上的输出:

Population (2011)
 • Total 86,685
86,685

相关问题 更多 >