Python基础

Posted zkbfighting

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础相关的知识,希望对你有一定的参考价值。

001:
from bs4 import BeautifulSoup
import requests

url = ‘http://news.sina.com.cn/china/‘
web_data = requests.get(url)
web_data.encoding = ‘utf-8‘
soup = BeautifulSoup(web_data.text,‘xml‘)

for news in soup.select(‘.news-item‘):
if(len(news.select(‘h2‘)) > 0):
h2 = news.select(‘h2‘)[0].text
time = news.select(‘.time‘)[0].text
a = news.select(‘a‘)[0][‘href‘].text
print(h2)
print(time)
print(a)
002:
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import urllib3
import sys
import chardet

req = urllib3.Request("http://www.163.com/")
content = urllib3.urlopen(req).read()
typeEncode = sys.getfilesystemencoding()
infoencode = chardet.detect(content).get(‘encoding‘,‘utf-8‘)
html = content.decode(infoencode,‘ignore‘).encode(typeEncode)
print (html)


# import urllib.request
#
# # 向指定的url发送请求,并返回服务器响应的类文件对象,urlopen中有data参数为POST请求,无data参数为GET请求
# response = urllib.request.urlopen("https://www.baidu.com/")
#
# # 类文件对象支持 文件对象的操作方法,如read()方法读取文件全部内容,返回字符串
# html = response.read()
#
# # 打印字符串
# print
# html


# import urllib.request
# resp=urllib.request.urlopen(‘http://www.baidu.com‘)
# html=resp.read()
# print(html)
003:
# #!/usr/bin/python2.6
# import requests
#
# from requests import HTMLSession
#
# session = HTMLSession()
#
# r = session.get("https://news.cnblogs.com/n/recommend")
#
# # 通过CSS找到新闻标签
# news = r.html.find(‘h2.news_entry > a‘)
#
# for new in news:
# print(new.text) # 获得新闻标题
# print(new.absolute_links) # 获得新闻链接


# m = ‘AFEFEFEaa‘
# print (m.lower())
# c=m.islower()
# print(c)
# print (m.upper())
#
# info = ‘babca‘
# print (info.find(‘a‘))
#
# info = ‘babca‘
# print (info.find(‘a‘,1))# 1代表查找字符串的起始位置,如果没有则返回-1


# info =
# info = ‘a‘:‘1‘,‘b‘:2,‘c‘:3,‘default‘:‘ss‘
# c = info.get(‘default‘,‘a‘)
# print (c)

#
# for i in range(5):
# if i==3:
# print (‘3‘)
# else:
# print(‘000000000‘)

# def ww():
# aa=100-10;
# return str(aa)+‘kg‘
#
# a=ww()
# print(a)
#
# def SUM(a,b):
# Count=a+b;
# return Count;
# aaa=SUM(1,input(‘4546‘))
# print(aaa)

#
# def text_create(name,msg):
# # 创建文件,写入信息
# desktop_path = ‘E:\\程序\\s

以上是关于Python基础的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python基础之函数

python 目录

python基础

人生苦短,我用Python(目录)

Python基础--Python3基础语法