使用Beautiful Soup和Splinter获取href和src属性的解决方法如下:
from bs4 import BeautifulSoup
from splinter import Browser
browser = Browser()
browser.visit(url)
html = browser.html
soup = BeautifulSoup(html, 'html.parser')
href_elements = soup.find_all(href=True)
for element in href_elements:
href = element['href']
print(href)
src_elements = soup.find_all(src=True)
for element in src_elements:
src = element['src']
print(src)
完整示例代码如下:
from bs4 import BeautifulSoup
from splinter import Browser
# 使用Splinter打开网页
browser = Browser()
browser.visit(url)
html = browser.html
# 使用Beautiful Soup解析HTML
soup = BeautifulSoup(html, 'html.parser')
# 获取所有的带有href属性的元素
href_elements = soup.find_all(href=True)
for element in href_elements:
href = element['href']
print(href)
# 获取所有的带有src属性的元素
src_elements = soup.find_all(src=True)
for element in src_elements:
src = element['src']
print(src)
注意:在运行代码之前,需要安装和配置好Beautiful Soup和Splinter库,并根据实际需求修改代码中的URL。