iT邦幫忙

2

Python 爬蟲不安全網站的問題

我要抓環保署中的資料
但是它是不安全的網站
我在我python 3.6 的 shell 執行下面的程式碼
是可以跑出東西的
如圖:
https://ithelp.ithome.com.tw/upload/images/20200924/201224637hlTB7A6pp.png

但是我額外儲存在一個.py程式檔再執行卻有錯誤

import requests
import urllib3
urllib3.disable_warnings()
r=requests.get("https://sta.ci.taiwan.gov.tw/STA_AirQuality_EPAIoT/v1.0/Datastreams?$expand=Thing,Observations($orderby=phenomenonTime%20desc;$top=1)&$filter=Thing/properties/city%20eq%20%27%E5%98%89%E7%BE%A9%E5%B8%82%27%20and%20name%20eq%27PM2.5%27&$count=true",verify=False)
               
print(r.content.decode('utf-8'))

錯誤:
https://ithelp.ithome.com.tw/upload/images/20200924/20122463WcPI8CZQGy.png

已經試過pip3 install urllib3 --upgrade
pip install --upgrade requests
但是都不行

請問要怎麼改進才能讓他可以執行?
或是有別的可以抓不安全網站的方式

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
japhenchen
iT邦超人 1 級 ‧ 2020-09-24 07:43:06

是該找個json parser來把json轉成陣列或字典啦
我改成下列,正常

import requests 
import os
import jsonpickle

url = 'https://sta.ci.taiwan.gov.tw/STA_AirQuality_EPAIoT/v1.0/Datastreams?$expand=Thing,Observations($orderby=phenomenonTime%20desc;$top=1)&$filter=Thing/properties/city%20eq%20%27%E5%98%89%E7%BE%A9%E5%B8%82%27%20and%20name%20eq%27PM2.5%27&$count=true'
r = requests.get(url,verify=False)
j = jsonpickle.decode(r.text)

for i in j['value'] :
    print(i) 

輸出
https://ithelp.ithome.com.tw/upload/images/20200924/201179540CFIjryc9T.jpg

lingwu iT邦新手 5 級 ‧ 2020-09-24 20:58:12 檢舉

變成出現module 'requests' has no attribute 'get'

有人說是重複命名requests.py
但是我找我的文件檔中都沒有出現requests.py

https://ithelp.ithome.com.tw/upload/images/20200924/20122463rwX8FKjFkt.png

lingwu iT邦新手 5 級 ‧ 2020-09-24 23:21:12 檢舉

可以了謝謝!!

skyksl066 iT邦新手 4 級 ‧ 2020-09-24 23:21:36 檢舉

套件壞掉了吧?
pip uninstall requests在重裝看看

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-09-24 09:00:53

把第三列urllib3.disable_warnings()刪掉再試試看
因為
錯誤訊息的最後一列寫著
AttributeError: module 'urllib3' has no attribute 'disable_warnings'

另外,點這裡是我這次鐵人賽唯一的一篇文章,喜歡的話左上角點 Like

lingwu iT邦新手 5 級 ‧ 2020-09-24 23:22:21 檢舉

謝謝你的建議
不過我發現好像是我的檔名叫ssl.py 所以有衝突

我要發表回答

立即登入回答