关于python的简单学习笔记
'''
导入模块,自带库:sys,re;第三方库:urllib3,xlwt,beautifulsoup
导入方式:①import XXX
②from XXX(某个库或文件夹) import XXX(某个库中的关键字或文件夹中的库)
获得网站的URL
获得网站HTML中的header信息和cookie
'''
from urllib import request,parse,error
url = 'http://httpbin.org/post'
headers={
'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'Host':'httpbin.org'
}
dict={
'Hello':'world'
}
data = bytes(parse.urlencode(dict),encoding='utf-8')
req = request.Request(url=url,data=data,headers=headers,method='POST')
response = request.urlopen(req)
print(response.read().decode('utf-8'))
'''
Tag 标签及其内容,且只拿它遇到的第一个
such as:title,head,div……
写法:bs.title
NavigableString 标签里的内容
写法:bs.title.string
bs.title.attrs 以字典形式输出获得标签里一一对应的键与值
BeautifulSoup 整个文件
Comment 注释
file = open(”./XXX“,"rb") 打开某文件,且以二进制的格式读取
html = file.read() 读这个文件
bs = BeautifulSoup(html,"html.parser") 用BeautifulSoup的html.parser解释器进行解释
'''