各位先進好,小弟剛自學python沒多久,最近在練習一個高鐵時刻表的爬蟲卻
一直卡關,不知道問題出在哪,用的是python3.6版
程式碼如下:
import requests
from bs4 import BeautifulSoup
url = 'http://www.thsrc.com.tw/tw/TimeTable/SearchResult'
form_data = {
'StartStation':'977abb69-413a-4ccf-a109-0272c24fd490',
'EndStation':'9c5ac6ca-ec89-48f8-aab0-41b738cb1814',
'DepartueSearchDate':'2019/01/30',
'DepartueSearchTime':'09:30',
'SearchWay':'DepartureInMandarin'
}
#原例子是用SearchDate,但在高鐵網頁按F12看到是DepartueSearchDate,不知有錯嗎?
response_post = requests.post(url, data=form_data)
soup_post = BeautifulSoup(response_post.text, 'lxml')
soup_post
原本要用下面的方式來記錄般車,但是用美麗湯解析後發現class裡面根本沒有column1
不知道問題出在哪?
# 所有班車(train_number)
td_col1 = soup_post.find_all('td', {'class':'column1'})
train_numbers = []
for tag in td_col1:
# print(tag)
train_number = tag.text
train_numbers.append(train_number)
有嘗試加header或是用urllib的方式,但好像都查詢不到
跪求高手解惑~謝謝!
原例:
https://github.com/music1353/pyHowFun/blob/master/LESSON4%20%E7%88%AC%E8%9F%B
2%E5%AF%A6%E6%88%B0%20-%20%E9%AB%98%E9%90%B5%E6%99%82%E5%88%BB.ipynb