---
# System prepended metadata

title: wk13_1130_Wikipedia

---

# 期末專題參考 

### {實戰#3} :  透過 Python 抓取 Wiki 頁面訊息
1. wikipedia 工具


```python
! pip install pycopy-webbrowser
```

    Defaulting to user installation because normal site-packages is not writeable
    Collecting pycopy-webbrowser
      Using cached pycopy-webbrowser-0.0.0.tar.gz (631 bytes)
    

    ERROR: pycopy-webbrowser from https://files.pythonhosted.org/packages/40/6f/3534aa5470f69aede4ec969a4f64981892e5bd838ea8e148d8d7ee52f1bc/pycopy-webbrowser-0.0.0.tar.gz does not appear to be a Python project: neither 'setup.py' nor 'pyproject.toml' found.
    


```python
import requests
from bs4 import BeautifulSoup
import webbrowser

while True :
    url = requests.get("http://zh.wikipedia.org/wiki/Special:Random") #網址為wikipedia特有的隨機搜尋功能
    soup = BeautifulSoup(url.content, "html.parser")
    title = soup.find(class_="firstHeading").text #class後面要加底線，否則會變成函數
    
    print(f"{title} \nDo you want to view it? (Y/N)")
    ans = input("").lower()
    
    if ans == "y" :
        url = "http://zh.wikipedia.org/wiki/%s" % title
        webbrowser.open(url)
        break
    elif ans == "n" :
        print("Try again !")
        continue
    else :
        print("Wrong choice !")
        continue
```

    私立广州法学院 
    Do you want to view it? (Y/N)
    n
    Try again !
    1896年夏季奧林匹克運動會射擊比賽－男子300米自由步槍 
    Do you want to view it? (Y/N)
    k
    Wrong choice !
    香港2006年6月 
    Do you want to view it? (Y/N)
    y
    
