|
发表于 2023-1-15 13:37:29
|
显示全部楼层
它这个页面也15日数据,前10天的在cityflowChart里可以用re拿到- import re
- from selenium.webdriver import Firefox
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as expected
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium.webdriver.firefox.options import Options
- options = Options()
- options.add_argument('-headless')
- driver = Firefox(options=options)
- wait = WebDriverWait(driver, 10)
- citys = ['beijing', 'shanghai', 'guangzhou', 'shenzhen', 'chengdu', 'nanjing', 'wuhan', 'chongqing', 'xian',
- 'tianjin']
- for city in citys:
- driver.get(f'https://metrodb.org/index/{city}.html')
- ele = wait.until(expected.visibility_of_element_located((By.ID, 'cityflowChart')))
- # print(ele.get_attribute("outerHTML"))
- s = ele.get_attribute("outerHTML")
- slst = re.findall(r'前10项是——(.*?)"', s)
- result = slst[0].split(',')
- print(result)
- driver.quit()
复制代码
但是后5天的数据,确实没有直接写在html里,如果用移动鼠标的办法,也比较麻烦。因为selenium移动鼠标要先找目的元素,而要找的后5天的柱状图并不是直观的元素,这个网页需要研究一下js,然后让python执行js,不过这部分我不熟,得等其他人看看了。 |
|