2019年10月7日 星期一

Python抓網路資料的兩種方法(Method:Get,Post),用GoodInfo做示範


Get抓GoodInfo
import requests
from bs4 import BeautifulSoup

# GoodInfo損益表--累計季表url = "https://goodinfo.tw/StockInfo/StockFinDetail.asp"url_paras = {'RPT_CAT': 'IS_M_QUAR_ACC', 'STOCK_ID': '2330'}
url_headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'}
r = requests.get(url, params=url_paras, headers=url_headers)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "lxml")
print(soup)

Post抓GoodInfo
import requests
from bs4 import BeautifulSoup

# GoodInfo損益表--年表url = "https://goodinfo.tw/StockInfo/StockFinDetail.asp"post_data = {'STEP': 'DATA', 'STOCK_ID': '2330', 'RPT_CAT': 'IS_M_YEAR', 'QRY_TIME': '2018'}
url_headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', \
    'referer': 'https://goodinfo.tw/StockInfo/StockFinDetail.asp?RPT_CAT=IS_M_QUAR_ACC&STOCK_ID=2330'}
r = requests.post(url, data=post_data, headers=url_headers)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "lxml")
print(soup)



沒有留言:

張貼留言