from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
def getLinks(articleUrl):
html = urlopen(" http://www.ccb.com/cn/home "+articleUrl)
bsObj = BeautifulSoup(html,'lxml')
print('bsObj.find=',bsObj.find("div", {"class":"Language_select"}).findAll("a"))
return bsObj.find("div", {"class":"Language_select"}).findAll("a",re.compile("^(<a href=\")(.*(?!\">繁体))$"))
links = getLinks("/indexv3.html")
print('links=',links)
输出如下:
bsObj.find= [<a href="http://fjt.ccb.com">繁体</a>, <a href="http://en.ccb.com/en/home/indexv3.html">ENGLISH</a>]
links= []
上面的代码用 BeautifulSoup 爬了" http://www.ccb.com/cn/home/indexv3.html ",输出的 a 标签内容里,“繁体”这两个字左侧的网址是我想要提取的网址, 即我希望输出的第二行应该是 links= ['http://fjt.ccb.com']。 现在看来 return 语句中的 findAll 没写对,导致输出为空,恳请大家指点应该怎么写才对呢?
感谢!