#爬虫
#1 url 爬取
import re
from urllib.request import urlopen
url='http://www.baidu.com'
resp=urlopen(url)
with open('test.html','w') as fp:
fp.write(resp.read().decode('utf-8'))
print('over')
#2 request模块
import requests
url='http://www.baidu.com'
resp=requests.get(url)
resp.encoding='utf-8'
print(resp.text)
#3 get请求 ctrl+F 调出搜索功能
content=input('请输入要检索的内容:')
url=f'http://www.sogou.com/web?query={content}'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}
resp=requests.get(url,headers=headers)
print(resp.text)
#4 post请求
import requests
url='https://fanyi.baidu.com/sug'
data={'kw':input('请输入一个单词:')}
resp=requests.post(url,data=data)
print(resp.json()['data'])
#5 get处理多参数url
import requests
url='https://movie.douban.com/j/chart/top_list'
lab={'type': '11',
'interval_id': '100:90',
'action':'',
'start': '0',
'limit':'20'}
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}
resp=requests.get(url,params=lab,headers=headers)
print(resp.json())
#6 正则表达式
'''
.匹配除换行符外所有
\w 匹配字母数字下划线 \W 匹配非
\d 匹配数字 一个元字符一位 \D 匹配非
^开始 $结尾
[a-zA-Z0-9]
[^a-zA-Z0-9]
# 量词 控制前面
*重复0次或更多次
+重复一次或更多次
?零次或一次
{n}重复n次
{n,}重复n次或更多次
{n,m}
#贪婪惰性
.*贪心
.*?惰性
'''
#7 re包
import re
a=re.findall('a','aafadaa