【问题标题】:How to webscrape the table data(includes merged cells) which have same class names using python and selenium如何使用 python 和 selenium 抓取具有相同类名的表数据(包括合并的单元格)
【发布时间】:2019-11-07 11:26:12
【问题描述】:

从图片来看,

2002 和 12 月和 1,262.516 -->> 在 html 标记中,2002 为 td[1],12 月为 td[2],1,262.516 为 td[3]

立即下一行,假设 2002 Q4 为 td [1] 4,017.422 td[2]

那么我如何将 4,017.422 存储在 domesitc 中?

https://i.stack.imgur.com/45F3v.png

如果有人知道请评论您的电子邮件ID。

这里是参考链接:https://www.transtats.bts.gov/freight.asp

【问题讨论】:

  • 可以分享html吗?
  • 可以分享你的邮件ID吗?
  • amitjain.coer191@gmail.com
  • //tr/td[text()='2002 Q4']//following-sibling::td[1]
  • 对于一个,这是可能的,在那个表中有很多像这样的合并数据,所以要做什么?

标签: python html css selenium


【解决方案1】:

您需要导入以下库。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

要获取所有元素,请尝试以下代码。 visibility_of_all_elements_located() 并遵循 xpath

allelements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH,"//td[@class='dataTD' and @colspan='2']/following::td[1]")))
for item in allelements:
    print(item.text)

或使用以下 css 选择器。

allelements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"td.dataTD[colspan='2']+td")))
for item in allelements:
    print(item.text)

** 预期输出**:

  Year        Month         Domestic

2003         December        1,424.216

2003Total       -           15,232.525

%Chg over 2002  -
Q4 Only                       5.13% 

2004        January         1,234.820   

已编辑 根据您的预期输出。这是更新的代码。 创建一个函数并传递字符串值。

def Get_details(strtext):
  Year=[]
  Month=[]
  Domestic=[]
  allelements=WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR,"td.dataTD")))
  for element in allelements:
     if element.text==strtext and element.get_attribute("colspan")=='2':
         Year.append(element.find_element_by_xpath("./parent::tr/preceding-sibling::tr[1]/td[1]").text)
         Month.append(element.find_element_by_xpath("./parent::tr/preceding-sibling::tr[1]/td[2]").text)
         Domestic.append(element.find_element_by_xpath("./parent::tr/preceding-sibling::tr[1]/td[3]").text)
         Year.append(element.text)
         Month.append("-")
         Domestic.append(element.find_element_by_xpath("./following::td[1]").text)
         if element.find_element_by_xpath("./parent::tr/following-sibling::tr[1]/td[1]").get_attribute("colspan")=='2':
             Year.append(element.find_element_by_xpath("./parent::tr/following-sibling::tr[1]/td[1]").text)
             Month.append("-")
             Domestic.append(element.find_element_by_xpath("./parent::tr/following-sibling::tr[1]/td[2]").text)
             Year.append(element.find_element_by_xpath("./parent::tr/following-sibling::tr[2]/td[1]").text)
             Month.append(element.find_element_by_xpath("./parent::tr/following-sibling::tr[2]/td[2]").text)
             Domestic.append(element.find_element_by_xpath("./parent::tr/following-sibling::tr[2]/td[3]").text)


  print(Year)
  print(Month)
  print(Domestic)

如果你用2003 Total调用函数

Get_details("2003 Total")

输出

['2003', '2003 Total', '%Chg over 2002\nQ4 Only', '2004']
['December', '-', '-', 'January']
['1,424.216', '15,232.525', '5.13%', '1,234.820']

如果你用2004 Total调用函数

Get_details("2004 Total")

输出

['2004', '2004 Total', '%Chg over 2003', '2005']
['December', '-', '-', 'January']
['1,526.984', '16,452.807', '8.01%', '1,280.516']

【讨论】:

  • year= [ 2003, 2003total,%Chg over 2002 Q4 Only,2004] Month =[ December, , , January] Domestic= [1,424, 15,232,5.13,1234 ]
  • @MonikRaj :更新了答案。
猜你喜欢
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多