# 1.1 環境安裝

因為我的電腦是MAC, 這邊就只記錄MAC的安裝方式. 至於Python版本, 這邊使用的是Python3.

**安裝 homebrew**:

參閱官網: <https://brew.sh/index_zh-tw.html>

**安裝 Python3:**

```bash
$ brew install python
$ brew install python3
```

Python 會被安裝在 **/usr/local/Cellar** 目錄之下

安裝好後, 你的 /usr/local/Cellar 目錄可能會長得類似這樣:

![](https://3902780279-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M523o3KiO0pmX6aiGw1%2F-M523rmaEjzd0w8x87jf%2F-M523sbj9fs_Uxyo_Qv_%2F1.1-1.png?generation=1587042036882849\&alt=media)

-> 注意python跟python3這兩個目錄.

**安裝virtualenv:**

虛擬環境的用意, 可以想成是一個沙盒, 不用擔心弄壞原本的環境, 也可以根據不同需求建立不同的虛擬環境.

```
$ pip install virtualenv
```

**建立一個virtual env:**

```
$ virtualenv -p python3 [env_name]
```

![](https://3902780279-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M523o3KiO0pmX6aiGw1%2F-M523rmaEjzd0w8x87jf%2F-M523sbl1_2wLhMmGMio%2F1.1-2.png?generation=1587042036373975\&alt=media)

建立好後, 來試著啟動虛擬環境吧:

```
$ source [env-name]/bin/activate
```

![](https://3902780279-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M523o3KiO0pmX6aiGw1%2F-M523rmaEjzd0w8x87jf%2F-M523sbnPctUcIMd-9rQ%2F1.1-3.png?generation=1587042037188193\&alt=media)

啟動成功的話, 可以在command line前面看到當前虛擬環境的名字(此處為web-crawler)

這時候查看一下python的版本, 會發現是python3:![](https://3902780279-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M523o3KiO0pmX6aiGw1%2F-M523rmaEjzd0w8x87jf%2F-M523sbpj-05AsxAIwhp%2F1.1-4.png?generation=1587042035865076\&alt=media)再來要安裝需要的套件, 如果你已經知道你要安裝什麼, 又懶得一個一個裝, 可以如下把所有要安裝的套件條列式的寫在一張[txt檔案](https://github.com/yotsuba1022/web-crawler-practice/blob/master/requirements.txt)裡面, 這邊我只列出這本gitbook裡面會需要用到的套件:

```
beautifulsoup4
requests
html5lib
matplotlib
selenium
jieba
wordcloud
Pillow==4.0.0
```

再來使用以下指令一次安裝所有套件:

```
$ pip install -r [txt_file_name]
```

安裝完後再確認一次:

```
$ pip list
```

![](https://3902780279-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M523o3KiO0pmX6aiGw1%2F-M523rmaEjzd0w8x87jf%2F-M523sbr4O4XUbZdOe_x%2F1.1-5.png?generation=1587042036164984\&alt=media)

這樣就安裝好虛擬環境了, 那麼要怎麼離開(deactivate)虛擬環境呢?

```
$ deactivate
```

![](https://3902780279-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M523o3KiO0pmX6aiGw1%2F-M523rmaEjzd0w8x87jf%2F-M523sbtmSQ1-SfYT4fr%2F1.1-6.png?generation=1587042036491559\&alt=media)

這樣就可以了, 很簡單吧\~

補充:

如果出現沒有切換至virtualenv就無法下python指令的情況, 在你的bash\_profile or bashrc裡面加入下面這項設定:

```
# DO NOT force vitualenv
export PIP_REQUIRE_VIRTUALENV=false
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://clu.gitbook.io/python-web-crawler-note/chapter1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
