无法提取Web内容href标签,我正在使用python 3.7

问题描述

无法从“ https://www.theaic.co.uk/aic/analyse-investment-companies”中抓取@href标签 我使用的是Python 3.7,草率,飞溅,还尝试了硒,但没有用。

解决方法

您在页面上看到的表位于<iframe>内,因此您必须先加载iframe的源:

import requests
from bs4 import BeautifulSoup

url = 'https://www.theaic.co.uk/aic/analyse-investment-companies'
soup = BeautifulSoup(requests.get(url).content,'html.parser')
soup = BeautifulSoup(requests.get('https:' + soup.article.iframe['src']).content,'html.parser')

for a in soup.select('.gridFundName a'):
    print(a['href'])

打印:

http://www.theaic.co.uk/3IN
http://www.theaic.co.uk/AAIF
http://www.theaic.co.uk/ADIG
http://www.theaic.co.uk/AEMC
http://www.theaic.co.uk/AJIT
http://www.theaic.co.uk/ALAI
http://www.theaic.co.uk/ABD
http://www.theaic.co.uk/ANII
http://www.theaic.co.uk/ANW
http://www.theaic.co.uk/ASCI
http://www.theaic.co.uk/AASC
http://www.theaic.co.uk/AAS
http://www.theaic.co.uk/ASEI
http://www.theaic.co.uk/ASLI
http://www.theaic.co.uk/ASL
http://www.theaic.co.uk/ASIT
http://www.theaic.co.uk/ASIZ
http://www.theaic.co.uk/AIF
http://www.theaic.co.uk/AIFZ
http://www.theaic.co.uk/AEWU