V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 提取这个 a 标签里的网址?

  •  
  •   saximi · Sep 1, 2017 · 11272 views
    This topic created in 3172 days ago, the information mentioned may be changed or developed.
    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 没写对,导致输出为空,恳请大家指点应该怎么写才对呢? 
    感谢! 
    
    
    13 replies    2017-09-03 12:07:22 +08:00
    impyf104
        1
    impyf104  
       Sep 1, 2017 via iPhone
    用 re.search 或者 re.findall 吧
    <a.*?href="(.*?)".*?>繁体</a>
    ossicee
        2
    ossicee  
       Sep 1, 2017 via Android
    a.get('href')
    saximi
        3
    saximi  
    OP
       Sep 1, 2017
    @impyf104 语句应该怎么写呢,我把 return 语句修改为:return re.findall('<a.*?href="(.*?)".*?>繁体</a>',bsObj.find("div", {"class":"Language_select"}).findAll("a"))

    但是运行报错如下

    File "D:\Python\Python3\lib\re.py", line 222, in findall
    return _compile(pattern, flags).findall(string)
    TypeError: expected string or bytes-like object
    saximi
        4
    saximi  
    OP
       Sep 2, 2017
    @impyf104 re.compile 为何不适用这种需求?
    impyf104
        5
    impyf104  
       Sep 2, 2017
    @saximi return re.findall(r'<a.*?href="(.*?)".*?>繁体</a>',html)[0]
    这样吧,[0]是满足正则的括号内的内容,具体的去查下怎么用吧,表达能力有限...
    yashirq
        6
    yashirq  
       Sep 2, 2017 via iPhone
    return bsObj.find("div", {“ class ”:” Language_select ”}.find("a", href=True)['href']
    OpenJerry
        7
    OpenJerry  
       Sep 2, 2017
    我感觉用正则有点复杂,这种情况我一般都是直接用 css 选择器

    ```python
    import requests
    from bs4 import BeautifulSoup
    import re

    def getLinks(articleUrl):
    html = requests.get("http://www.ccb.com/cn/home"+articleUrl).text
    bsObj = BeautifulSoup(html,'lxml')
    fanti_link = bsObj.select(".Language_select a")[0]['href']
    return fanti_link

    links = getLinks("/indexv3.html")
    print('links=',links)
    ```
    saximi
        9
    saximi  
    OP
       Sep 2, 2017
    @yashirq 谢谢,请问 href=True 表示什么含义呢?
    saximi
        10
    saximi  
    OP
       Sep 2, 2017
    @yashirq href=True 表示什么含义,以及为何用['href']键就能取到第一个链接? 顺便问一下,如果要取第二个链接,应该用什么下标呢?谢谢
    VitaCoCo
        11
    VitaCoCo  
       Sep 2, 2017
    for a in bsObj.find("div", {"class":"Language_select"}).findAll("a")):
    print a.attr("href")
    yashirq
        12
    yashirq  
       Sep 3, 2017
    @saximi find(name, attrs, recursive, string, **kwargs) href=True 指的是找 bsobj 下有 href 这个属性的 object ;取到第一个链接不是因为用[href'],而是因为 find ()返回的就是第一个 object,findall ()返回的所有包含 href 的 object ;取第二个链接可以用 find_next(),或者 findall(limit=2),可以看看官方文档的介绍 https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find
    saximi
        13
    saximi  
    OP
       Sep 3, 2017
    @yashirq 感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2993 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 07:23 · PVG 15:23 · LAX 00:23 · JFK 03:23
    ♥ Do have faith in what you're doing.