import requests
r = requests.get(url='http://learncodethehardway.org/words.txt') # 最基本的GET请求
print(r.status_code) # 获取返回状态
r = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'}) # 带参数的GET请求
print(r.url)
print(r.text) # 打印解码后的返回数据
输出结果:
D:\Python\Python36\python.exe E:/pytest/ex41c.py
200
http://dict.baidu.com/s?wd=python
<!Doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
...
...
</script> </div>
</body>
</html>
Process finished with exit code 0
from urllib.request import urlopen
for line in urlopen('http://learncodethehardway.org/words.txt'):
line = line.decode('utf-8')
if 'app' in line:
print(line)
输出结果:
D:\Python\Python36\python.exe E:/pytest/ex41c.py apparatus apparel apple appliance approval Process finished with exit code 0
浙公网安备 33010602011771号