Getting attribute's value using BeautifulSoup

后端 未结 3 2059
予麋鹿
予麋鹿 2020-12-30 10:57

I\'m writing a python script which will extract the script locations after parsing from a webpage. Lets say there are two scenarios :



        
3条回答
  •  鱼传尺愫
    2020-12-30 11:20

    This should work, you just filter to find all the script tags, then determine if they have a 'src' attribute. If they do then the URL to the javascript is contained in the src attribute, otherwise we assume the javascript is in the tag

    #!/usr/bin/python
    
    import requests 
    from bs4 import BeautifulSoup
    
    # Test HTML which has both cases
    html = '  '
    
    soup = BeautifulSoup(html)
    
    # Find all script tags 
    for n in soup.find_all('script'):
    
        # Check if the src attribute exists, and if it does grab the source URL
        if 'src' in n.attrs:
            javascript = n['src']
    
        # Otherwise assume that the javascript is contained within the tags
        else:
            javascript = n.text
    
        print javascript
    

    This output of this is

    http://example.com/something.js
    some JS
    

提交回复
热议问题