1. 根据id属性获取,并读取src对应的链接。
pic_info = obj.find_all('img', attrs={'id': "imgpath"})
url_pic = pic_info[0]['src']
或者
pic_vlaue = single_value[1].find('img')['src']
2. 根据class属性获取,并读取href对应的链接。
pic_info = obj.find_all('div', attrs={'class': "alarm"})
warningInformation = pic_info[0].find_all('h2')
warning_list = []
for line in warningInformation:
one_warning_list = []
title = line.find_all('a')[0].text
link = line.find_all('a')[0].attrs['href']
3.获取星兄弟的数据
ss = ul.next_sibling.next_sibling.text.strip()
4.selenium 获取某个元素的html
4.1 定位及其获取文本
abstract_content = browser.find_element_by_id("alarmcontent").text
html_content = browser.find_element_by_class_name("rainstormRecoveryDirectoryBox")
html_text = html_content.get_attribute('outerHTML')
4.2 获取内部html:jquey的innerHtml():
get_attribute('innerHTML')
获取整体html:jquey的html():
get_attribute('outerHTML')
4.3 截取自定义窗口的图片
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.set_page_load_timeout(480)
browser.get(URL)
canvas = browser.find_element_by_id("livecanvas")
num = 30 # 尝试截取num次
while num > 0:
as_png = canvas.screenshot_as_png # 获取截图的字节流
# 如果截取的是空白图片,大小为5KB左右,截取正确图片大小为700KB左右
if len(as_png) > 1024 * 20:
browser.quit()
binary_video = as_png
break
num -= 1
if num == 0: # 截取num次后还没有得到有效图片,抛异常
browser.quit()
binary_video = ''
break
time.sleep(1)
return binary_video
网友评论