---
# System prepended metadata

title: NetworkProgram

---

# NetworkProgram

## [urllib](https://docs.python.org/zh-tw/3/howto/urllib2.html)

- urllib -> URL libary


```python=

# urllib.request get web data
import urllib.request as request

src ="https://www.ntu.edu.tw/"

#to get the data from the target
with request.urlopen(src) as responce:
    data = responce.read().decode("utf-8") # get the target SourseCode
    
print(data)
```
Remark : under the bash will occur the problem


### Get the json(dict) file (API)


```python=
import urllib.request as request
import json

src="https://data.taipei/api/v1/dataset/296acfa2-5d93-4706-ad58-e83cc951863c?scope=resourceAquire"

with request.urlopen(src) as response:
    data = json.load(response)

print(data)

```

### Select data that you want
```python=
import urllib.request as request
import json

src="https://data.taipei/api/v1/dataset/296acfa2-5d93-4706-ad58-e83cc951863c?scope=resourceAquire"


with request.urlopen(src) as response:
    data =json.load(response)



CatchList=data["result"]["results"]  #分析 json key->value 
for company in CatchList:
    print(company["公司名稱"])
 
```
### Write into the .txt

```python=
import urllib.request as request
import json

src="https://data.taipei/api/v1/dataset/296acfa2-5d93-4706-ad58-e83cc951863c?scope=resourceAquire"


with request.urlopen(src) as response:
    data =json.load(response)


with open("company.txt","w",encoding="utf-8") as file:  # data write into the file.txt
    CatchList=data["result"]["results"]  #分析 json key->value 
    for company in CatchList:
        print(company["公司名稱"])
        file.write(company["公司名稱"]+"\n")



#with open("data.txt","w",encoding="utf-8") as file:
```



