1、vars() 查看一个.py文件中的全局变量
print(vars()) #重点 __name__': '__main__ '__file__': 'C:/Users/lenovo/PycharmProjects/老男孩/pppppppp.py' {'__spec__': None, '__name__': '__main__', '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/lenovo/PycharmProjects/老男孩/pppppppp.py',
'__package__': None, '__cached__': None, '__doc__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000000AFF7E53E10>}
1> __doc__ 查看.py文件的注释
2> __file__ 本身.py文件的绝对路径
import os
print(__file__) #当前路径
print(os.path.dirname(__file__)) #上一级目录 C:/Users/lenovo/PycharmProjects/老男孩/pppppppp.py
C:/Users/lenovo/PycharmProjects/老男孩
把一个路径导入到sys.pash中
import os,sys
print(sys.path) #查看导入模块时所寻找的路径
print(__file__)
a=os.path.dirname(__file__) #利用__file__获取当前文件的绝对路径
print(os.path.dirname(__file__)) #利用os.pash.dirname(__file__路径)得到当前绝对路径的上一级目录
print(os.path.join(a,"mokuai")) #利用os.pash.join()把俩个路径拼接起来
b=os.path.join(a,"mokuai")
sys.path.append(b) #把拼接后的路径加到寻找模块时查看的路径中
print(sys.path) #查看当前需要导入模块时所寻找的路径 ['C:\\Users\\lenovo\\PycharmProjects\\老男孩', 'C:\\Users\\lenovo\\PycharmProjects\\老男孩', 'E:\\Python35\\python35.zip', 'E:\\Python35\\DLLs', 'E:\\Python35\\lib', 'E:\\Python35', 'E:\\Python35\\lib\\site-packages']
C:/Users/lenovo/PycharmProjects/老男孩/pppppppp.py
C:/Users/lenovo/PycharmProjects/老男孩
C:/Users/lenovo/PycharmProjects/老男孩\mokuai
['C:\\Users\\lenovo\\PycharmProjects\\老男孩', 'C:\\Users\\lenovo\\PycharmProjects\\老男孩', 'E:\\Python35\\python35.zip', 'E:\\Python35\\DLLs', 'E:\\Python35\\lib', 'E:\\Python35', 'E:\\Python35\\lib\\site-packages', 'C:/Users/lenovo/PycharmProjects/老男孩\\mokuai']
3>__name__ 如果是在自己的.py文件中__name__=="__main__",而如果是在一个文件中导入另外一个文件的话,打印另外一个文件的__name__就成了那个文件的文件名
常利用__name__=="__main__"来定义一个主文件
import pppppppp
print(__name__)
print(pppppppp.__name__) __main__
pppppppp
定义主文件
if __name__=="__main__":
print("123")
2、urllib模块
import urllib #导入urllib模块
from urllib import request #导入urllib中request方法
f=request.urlopen("http://ing.cnblogs.com/") #输入网址发送请求
a=f.read().decode("utf-8") #输入并编码
print(a)
查询各地天气的代码
import urllib #导入urllib模块
from urllib import request #导入urllib中request方法
f=request.urlopen("http://www.weather.com.cn/adat/sk/101050101.html") #输入网址发送请求
a=f.read().decode("utf-8") #输入并编码
print(a)
3、json模块
json,用于字符串 和 python数据类型间进行转换
Json模块提供了四个功能:dumps、dump、loads、load
json.loads()用于将字符串形式的字典,列表转换成相应的字典,列表(元祖不可以,应为其他语言中没用元祖)
import json
a="[1,2,3,4]"
b='{"k1":1,"k2":2}' #当字符串为字典时{}外面必须是''单引号{}里面必须是""双引号
print(json.loads(a))
print(b) [1, 2, 3, 4]
{"k1":1,"k2":2}
json.dumps()字典,列表转换成相应的字符串(元祖转换的时候转成字符串形式的列表)
import json
a=[1,2,3,4]
b={"k1":1,"k2":2}
print(json.dumps(a))
print(json.dumps(b)) [1, 2, 3, 4]
{"k1": 1, "k2": 2}
json.dump()把一个字典或列表写到一个文件中写成字符串的形式
import json
a=[1,2,3,4]
b=json.dump(a,open("db","a"))
json.load()把一个文件中的字符串以字典或列表的形式读出来
import json
b=json.load(open("db","r"))
print(b)
4.request(请求的意思)
用于发送http请求的,用python模拟浏览器浏览网页
import requests #导入request模块
a=requests.get("http://www.weather.com.cn/adat/sk/101050101.html") #发送url地址并获取返回值a
a.encoding="utf-8" #把获取到的内容进行编码
b=a.text #text返回的内容
print(b)
5、XML
获取QQ是否在线
import requests #导入request模块
a=requests.get('http://www.webxml.com.cn//webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=424662508') #发送请求并获取返回值
a.encoding="utf-8" #编码
b=a.text #读出内容
print(b) #查看读出的内容属于什么类型
from xml.etree import ElementTree as f #获取的字符串是xml格式的所以用xml方法
a=f.XML(b) #用xml进行提取内容
c=a.text #提取到内容后进行读取
print(c) <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">Y</string>
Y
列车时刻表
import requests
a=requests.get('http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=k234&UserID=')
a.encoding="utf-8"
b=a.text
print(b)
from xml.etree import ElementTree as f
a=f.XML(b)
print(a.tag) #获取第一个节点的标签
for i in a.iter("TrainDetailInfo"): #找到某一类节点进行循环 通过iter迭代TrainDetailInfo 我们可以把它看成一个列表
print(i) #通过打印获取一个迭代的对象
print(i.tag,i.attrib) #i.tag是获取节点的标签,i.attrib是获取标签的属性
print(i.find("TrainStation").text) #在TrainDetailInfo节点下找TrainStation标签
print(i.find("StartTime").text) #在TrainDetailInfo节点下找StartTime标签 def iter(self, tag=None):
在当前节点的子孙中根据节点名称寻找所有指定的节点,并返回一个迭代器(可以被for循环)。
<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://WebXml.com.cn/">
<xs:schema id="getDetailInfo" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="getDetailInfo" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="" maxOccurs="unbounded">
<xs:element name="TrainDetailInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="TrainStation" type="xs:string" minOccurs="" />
<xs:element name="ArriveTime" type="xs:string" minOccurs="" />
<xs:element name="StartTime" type="xs:string" minOccurs="" />
<xs:element name="KM" type="xs:short" minOccurs="" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<getDetailInfo xmlns="">
<TrainDetailInfo diffgr:id="TrainDetailInfo1" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>上海(车次:K234\K235)</TrainStation>
<ArriveTime />
<StartTime>11:12:00</StartTime>
<KM>0</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo2" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>昆山</TrainStation>
<ArriveTime>11:45:00</ArriveTime>
<StartTime>11:48:00</StartTime>
<KM>49</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo3" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>苏州</TrainStation>
<ArriveTime>12:12:00</ArriveTime>
<StartTime>12:16:00</StartTime>
<KM>84</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo4" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>无锡</TrainStation>
<ArriveTime>12:44:00</ArriveTime>
<StartTime>12:55:00</StartTime>
<KM>126</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo5" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>常州</TrainStation>
<ArriveTime>13:22:00</ArriveTime>
<StartTime>13:26:00</StartTime>
<KM>165</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo6" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>镇江</TrainStation>
<ArriveTime>14:13:00</ArriveTime>
<StartTime>14:16:00</StartTime>
<KM>237</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo7" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>南京</TrainStation>
<ArriveTime>15:04:00</ArriveTime>
<StartTime>15:16:00</StartTime>
<KM>301</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo8" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>蚌埠</TrainStation>
<ArriveTime>17:27:00</ArriveTime>
<StartTime>17:50:00</StartTime>
<KM>485</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo9" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>徐州</TrainStation>
<ArriveTime>19:38:00</ArriveTime>
<StartTime>19:58:00</StartTime>
<KM>649</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo10" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>商丘</TrainStation>
<ArriveTime>22:12:00</ArriveTime>
<StartTime>22:17:00</StartTime>
<KM>795</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo11" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>开封</TrainStation>
<ArriveTime>23:49:00</ArriveTime>
<StartTime>23:53:00</StartTime>
<KM>926</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo12" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>郑州</TrainStation>
<ArriveTime>00:37:00</ArriveTime>
<StartTime>01:14:00</StartTime>
<KM>998</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo13" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>新乡</TrainStation>
<ArriveTime>02:20:00</ArriveTime>
<StartTime>02:22:00</StartTime>
<KM>1078</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo14" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>鹤壁</TrainStation>
<ArriveTime>03:01:00</ArriveTime>
<StartTime>03:03:00</StartTime>
<KM>1144</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo15" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>安阳</TrainStation>
<ArriveTime>03:33:00</ArriveTime>
<StartTime>03:36:00</StartTime>
<KM>1185</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo16" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>邯郸</TrainStation>
<ArriveTime>04:11:00</ArriveTime>
<StartTime>04:16:00</StartTime>
<KM>1245</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo17" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>邢台</TrainStation>
<ArriveTime>04:47:00</ArriveTime>
<StartTime>04:51:00</StartTime>
<KM>1297</KM>
</TrainDetailInfo>
<TrainDetailInfo diffgr:id="TrainDetailInfo18" msdata:rowOrder="" diffgr:hasChanges="inserted">
<TrainStation>石家庄</TrainStation>
<ArriveTime>06:05:00</ArriveTime>
<StartTime />
<KM>1406</KM>
</TrainDetailInfo>
</getDetailInfo>
</diffgr:diffgram>
</DataSet>
<Element 'TrainDetailInfo' at 0x000000E68985D2C8>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo1', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
上海(车次:K234\K235)
11:12:00
<Element 'TrainDetailInfo' at 0x000000E68985D458>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo2', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
昆山
11:48:00
<Element 'TrainDetailInfo' at 0x000000E68985D5E8>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo3', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
苏州
12:16:00
<Element 'TrainDetailInfo' at 0x000000E68985D778>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo4', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
无锡
12:55:00
<Element 'TrainDetailInfo' at 0x000000E68985D908>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo5', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
常州
13:26:00
<Element 'TrainDetailInfo' at 0x000000E68985DA98>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo6', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
镇江
14:16:00
<Element 'TrainDetailInfo' at 0x000000E68985DC28>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo7', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
南京
15:16:00
<Element 'TrainDetailInfo' at 0x000000E68985DDB8>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo8', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
蚌埠
17:50:00
<Element 'TrainDetailInfo' at 0x000000E68985DF48>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo9', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
徐州
19:58:00
<Element 'TrainDetailInfo' at 0x000000E68986A138>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo10', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
商丘
22:17:00
<Element 'TrainDetailInfo' at 0x000000E68986A2C8>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo11', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
开封
23:53:00
<Element 'TrainDetailInfo' at 0x000000E68986A458>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo12', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
郑州
01:14:00
<Element 'TrainDetailInfo' at 0x000000E68986A5E8>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo13', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
新乡
02:22:00
<Element 'TrainDetailInfo' at 0x000000E68986A778>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo14', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
鹤壁
03:03:00
<Element 'TrainDetailInfo' at 0x000000E68986A908>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo15', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
安阳
03:36:00
<Element 'TrainDetailInfo' at 0x000000E68986AA98>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo16', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
邯郸
04:16:00
<Element 'TrainDetailInfo' at 0x000000E68986AC28>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo17', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
邢台
04:51:00
<Element 'TrainDetailInfo' at 0x000000E68986ADB8>
TrainDetailInfo {'{urn:schemas-microsoft-com:xml-diffgram-v1}hasChanges': 'inserted', '{urn:schemas-microsoft-com:xml-diffgram-v1}id': 'TrainDetailInfo18', '{urn:schemas-microsoft-com:xml-msdata}rowOrder': ''}
石家庄
None
import requests
a=requests.get("http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?TrainCode=K234&UserID=")
a.encoding="utf-8"
b=a.text
print(b)
from xml.etree import ElementTree as f
c=f.XML(b)
for i in c.iter("TrainDetailInfo"):
print(i)
print(i.tag,i.attrib)
print(i.find("TrainStation").text,i.find("ArriveTime").text,i.find("StartTime").text,i.find("KM").text) 上海(车次:K234\K235) None 11:12:00 0
昆山 11:45:00 11:48:00 49
苏州 12:12:00 12:16:00 84
无锡 12:44:00 12:55:00 126
常州 13:22:00 13:26:00 165
镇江 14:13:00 14:16:00 237
南京 15:04:00 15:16:00 301
蚌埠 17:27:00 17:50:00 485
徐州 19:38:00 19:58:00 649
商丘 22:12:00 22:17:00 795
开封 23:49:00 23:53:00 926
郑州 00:37:00 01:14:00 998
新乡 02:20:00 02:22:00 1078
鹤壁 03:01:00 03:03:00 1144
安阳 03:33:00 03:36:00 1185
邯郸 04:11:00 04:16:00 1245
邢台 04:47:00 04:51:00 1297
石家庄 06:05:00 None 1406