V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
saximi
V2EX  ›  Python

请教用 BeautifulSoup 和正则表达式爬取网站的两个问题

  •  
  •   saximi · 2017-08-30 20:31:16 +08:00 · 1570 次点击
    这是一个创建于 2437 天前的主题,其中的信息可能已经有所发展或是发生改变。
    1、代码一的问题 
    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=",bsObj) 
        return bsObj.find("div", {"class":"copy-right_text"}).findAll("span",re.compile("^'手机网站:'(.)*$")) 
      
    links = getLinks("/indexv3.html") 
    print(links) 
    
    
    上面的代码一是用于爬“ http://www.ccb.com/cn/home/indexv3.html ” 这个网址底部“手机网站”栏位显示的网址,打印 BeautifulSoup(html,'lxml') 返回的对象时发现“手机网站”这几个字并未出现。 
    通过查看网站源,发现程序未显示的内容都是在网站接近末尾处的这句话之后:“<!--底栏下面不可跳转部分-->”,这句话之后的网站源码无法被 BeautifulSoup.find 搜索到, 
    请问这是为什么呢?要如何才能查到呢?感谢! 
    
    
    2、代码二的问题 
    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"})) 
        return bsObj.find("div", {"class":"Language_select"}).findAll("a",href=re.compile("\"( http://.* )\">繁体")) 
    
    links = getLinks("/indexv3.html") 
    print('links=',links) 
    
    上面的代码二是用于爬“ http://www.ccb.com/cn/home/indexv3.html ” 这个网址底部繁体网站的域名,程序输出如下: 
    
    bsObj.find= 
     
    http://fjt.ccb.com ">繁体 /http://en.ccb.com/en/home/indexv3.html ">ENGLISH 
    
    
    links= [] 
    
    输出中的 http://fjt.ccb.com 就是希望提取的结果,但是为何最终打印 links 却没有内容呢?恳请指点!感谢! 
    
    
    1 条回复    2017-09-01 00:28:38 +08:00
    saximi
        1
    saximi  
    OP
       2017-09-01 00:28:38 +08:00
    自己顶一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2428 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 13:54 · PVG 21:54 · LAX 06:54 · JFK 09:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.