12306就算了,爬一下台湾的高铁试试:
首先到台湾高铁查一下台北到桃园站的地铁。
打开浏览器开发者模式,找到表单数据,把表单用字典按键值对方式的方式存入变量payload
用post方法把网址和表单数据发出。
代码:
import requests
from bs4 import BeautifulSoup
payload = {
"StartStation":"977abb69-413a-4ccf-a109-0272c24fd490",
"EndStation":"fbd828d8-b1da-4b06-a3bd-680cdca4d2cd",
"SearchDate":"2017/11/29",
"SearchTime":"13:30",
"SearchWay":"DepartureInMandarin"
}
res = requests.post("http://www.thsrc.com.tw/tw/TimeTable/SearchResult",data = payload)
soup = BeautifulSoup(res.text)
for item in soup.select('.column1'):
print(item.text)
for item in soup.select('.column2'):
print(item.text)
for item in soup.select('.column3'):
print(item.text)
for item in soup.select('.column4'):
print(item.text)
得到的结果:
車次
0645
0833
0651
0837
0657
0841
0661
0663
0845
0667
行車時間
00:22
00:21
00:22
00:21
00:22
00:21
00:20
00:22
00:21
00:20
出發時間
13:46
14:11
14:46
15:11
15:46
16:11
16:21
16:46
17:11
17:21
抵達時間
14:08
14:32
15:08
15:32
16:08
16:32
16:41
17:08
17:32
17:41