#當標籤中含特殊字元時, 使用 dict 取得元件

#當 key 含特殊字元時, 使用 dict 取得元件  
import requests
from bs4 import BeautifulSoup
def main():
    resp = requests.get('http://jwlin.github.io/py-scraping-analysis-book/ch2/blog/blog.html')
    soup = BeautifulSoup(resp.text, 'html.parser')
#抓取 <a data-foo="mac-foo" href="http://www.pycone.com/blogs/mac-python-environment"> <br/>Read More </a>
    #print(soup.find(data-foo='mac-foo'))  # 無法直接抓,會導致 SyntaxError
 
    print(soup.find('a', {'data-foo': 'mac-foo'}))#需使用dict 取得元件 
    print(soup.find('a', {'data-foo': 'mac-foo'}).text)
  
if __name__ == '__main__':
    main()

Yiru@Studio - 關於我 - 意如