Get the operation status of JR West with Python

1_ Get the operation status of JR West 2_ Get a route with a delay 3_ output To do.

[Operating status quote source] http://trafficinfo.westjr.co.jp/list.html

Only the operation status is obtained from html using xpath. I used lxml in the process here. It's not particularly difficult, so is it for scraping practice? May be just right for you. It also looks like an iPhone when accessed by a user agent. Since it is received as an argument, it can be changed freely. Also, if there is no delay, the function will end the process as it is without returning (because it is if body :).

Output sample

[01:26 Current operating status]
Chugoku area:There is a delay etc.

code

GET_JR_INFO.py


#coding: utf-8

import urllib2, lxml.html
from datetime import datetime
	
def GET_info(user_agent):
	headers = {'User-Agent': user_agent}
	root = lxml.html.fromstring(urllib2.urlopen(urllib2.Request("http://trafficinfo.westjr.co.jp/list.html", None, headers)).read())

	list_data = []
	list_data.append("[{}]".format("".join([i.text for i in root.xpath('//*[@id="contents"]/div[1]/h1')]).encode("utf-8")))
	for i in range(1, 7):
		path_status = '//*[@id="contents"]/div[2]/ul/li[%d]/span[3]' % i
		path_name = '//*[@id="contents"]/div[2]/ul/li[%d]/span[1]' % i
		if not "There is no delay information." in "".join([i.text for i in root.xpath(path_status)]).encode("utf-8"):
			body = "{}: {}".format(
				"".join([i.text for i in root.xpath(path_name)]).encode("utf-8"),
				"".join([i.text for i in root.xpath(path_status)]).encode("utf-8"))
			list_data.append(body)
	if body:
		return list_data

if __name__ == "__main__":
	print "\n".join(GET_info(
		user_agent="Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25"
		))

Recommended Posts

Get the operation status of JR West with Python
Get the weather with Python requests
Get the weather with Python requests 2
Try to automate the operation of network devices with Python
Get the source of the page to load infinitely with python.
Check the existence of the file with python
[Python] Get the variable name with str
[Python] Get the character code of the file
PhytoMine-I tried to get the genetic information of plants with Python
Get the width of the div on the server side with Selenium + PhantomJS + Python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
I tried to get the authentication code of Qiita API with Python.
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
Get and estimate the shape of the head using Dlib and OpenCV with python
Get the return value of an external shell script (ls) with python3
[Python] Get the files in a folder with Python
Prepare the execution environment of Python3 with Docker
Automatic operation of Chrome with Python + Selenium + pandas
2016 The University of Tokyo Mathematics Solved with Python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
Calculate the total number of combinations with python
Check the date of the flag duty with Python
[Python] Get rid of dating with regular expressions
Convert the character code of the file with Python3
[Python] Get the day of the week (English & Japanese)
[Python] Determine the type of iris with SVM
Get the update date of the Python memo file.
[Python] Operation of enumerate
Get date with python
Periodically notify the processing status of Raspberry Pi with python → Google Spreadsheet → LINE
Note: How to get the last day of the month with python (added the first day of the month)
Get the stock price of a Japanese company with Python and make a graph
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the text of the law from the e-GOV Law API
[Python] Get the numbers in the graph image with OCR
Destroy the intermediate expression of the sweep method with Python
[python] Get the list of classes defined in the module
Visualize the range of interpolation and extrapolation with python
Get the return code of the Python script from bat
Calculate the regression coefficient of simple regression analysis with python
Get the result in dict format with Python psycopg2
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
Understand the status of data loss --Python vs. R
Visualize the appreciation status of art works with OpenCV
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Extract the band information of raster data with python
Get the value of a specific key in a list from the dictionary type in the list with Python
Build API server for checking the operation of front implementation with python3 and Flask
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
Get country code with python
Towards the retirement of Python2
Get Twitter timeline with python