[问题] 高铁网站回应出现405错误讯息 (已解决)

楼主: gg (GG)   2021-05-06 09:26:19
[已解决,附上正确程式码。]
[目前高铁仍没有挡 "未经过伪装的" headers, 但是底下为了完整性仍附上 headers]
我根据 
https://github.com/jwlin/py-scraping-analysis-book/blob/master/ch7/thrsc.py
的范例程式码,以高铁网站测试 requests post 方法,
由于网站改版,因此修改程式码如下:
import requests
from bs4 import BeautifulSoup
headers ={
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
def get_station_id(headers=headers):
URL = 'https://www.thsrc.com.tw/tw/TimeTable/SearchResult'
station_to_id = {}
resp = requests.get(URL, headers=headers)
soup = BeautifulSoup(resp.text, 'html5lib')
for opt in soup.find('select', id='select_location01').find_all('option'):
station_to_id[opt.text.strip()] = opt['value']
return station_to_id
def get_times(start_station, end_station, search_date, search_time, headers=headers):
URL = 'https://www.thsrc.com.tw/TimeTable/Search'
form_data = {
'SearchType': 'S',
'Lang': 'TW',
'StartStation': station_to_id[start_station],
'EndStation': station_to_id[end_station],
'OutWardSearchDate': search_date,
'OutWardSearchTime': search_time,
'ReturnSearchDate': search_date,
'ReturnSearchTime': search_time,
'DiscountType': ''
}
resp = requests.post(URL, data=form_data, headers=headers)
print(resp)
data = resp.json()
# import json
# with open('test' + '.json', 'w', encoding='utf-8') as f:
# json.dump(data, f, indent=2, sort_keys=True, ensure_ascii=False)
columns = ['TrainNumber', 'DepartureTime', 'DestinationTime', 'Duration']
times = []
for d in data['data']['DepartureTable']['TrainItem']:
times.append([d[c] for c in columns])
return times
if __name__ == '__main__':
station_to_id = get_station_id()
times = get_times('台北', '左营', '2021/05/10', '10:30')
print('车次, 出发时间, 抵达时间, 行车时间')
for t in times:
print(t)
使用这个程式码可以用 get 方法获取完整的 station_to_id,
但是 resp 会出现 <Response [405]>, 因此 post 方法收不到任何东西。
请问:以这个例子而言,405错误讯息的解法为何?
作者: melancholy07 (雾雨)   2021-05-06 13:56:00
我还没看post的data格式跟内容有没有问题但你要不要先带headers再试试看?
作者: pinefruit (莫使惹尘埃)   2021-05-06 14:58:00
你的 SearchType 是不是忘了填值?
作者: lycantrope (阿宽)   2021-05-06 16:02:00
确定url没错?url好像又好了.

Links booklink

Contact Us: admin [ a t ] ucptt.com