
#coding=utf-8
myList=[]
for i in range(5)
myList[i]=input() #每个元素都是字符串,要别的类型需要自己转换
print myList
关键在于查找时间的正则表达式,也就是程序中reg变量的字符串,你可以去了解一下
import res = """<a class="time" target="_blank" href="">昨天 00:26</a>
<a class="time" target="_blank" href="">今天 00:26</a>"""
def getTime(html):
reg = r'<a class="time">()</a>'
timere = recompile(reg)
timelist = refindall(timere,html)
for t in timelist:
print t
getTime(s)
你的题目我没有看懂,什么一行相加等于5的所有行标。
我这边举一个其他的例子,你可以仿照一下:
查找所有以a开头的行号,并打印出来
# cat testtxtabc
hgg
elk
aaaaf
bce
ssiufuisfuif
脚本如下:
# -- coding:utf-8 --import re
f=open('testtxt', 'r')
n = 1
for i in freadlines():
# 判断条件你可以自己修改
if rematch('a', str(isplit()[0])):
print n,isplit()[0]
n = n + 1
文件不大的话,直接使用read()读取文件,利用字符串函数count()统计字符出现的次数:
哦,对了,“The Zen of Python”的内容是如下方式得到了:
html_doc = """
<html>
<head>
<title>The Dormouse's story</title>
</head>
<body>
<p class="title aq">
<b>
The Dormouse's story
</b>
</p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href=">
您好,Python可以使用多种方式来解析XHTML文档,其中最常用的方式是使用第三方库BeautifulSoup。BeautifulSoup是一个基于Python的HTML/XML解析器,它可以帮助开发者快速解析XHTML文档。
使用BeautifulSoup解析XHTML文档的步骤如下:
1 安装BeautifulSoup库。可以使用pip命令来安装:pip install beautifulsoup4。
2 导入BeautifulSoup库。在Python代码中使用from bs4 import BeautifulSoup语句导入库。
3 读取XHTML文档。使用Python的文件 *** 作函数打开文件,读取文件内容。
4 创建BeautifulSoup对象。使用BeautifulSoup类创建一个对象,传入读取的XHTML文档内容和解析器类型。
5 解析XHTML文档。使用BeautifulSoup对象的方法来解析XHTML文档,例如find、find_all等方法。
6 处理解析结果。根据解析结果来处理XHTML文档的内容,例如提取需要的信息、修改XHTML文档等。
总之,Python可以使用BeautifulSoup等第三方库来解析XHTML文档,这些库提供了简单易用的API,使得开发者可以快速解析和处理XHTML文档。
python爬虫项目实战:
爬取糗事百科用户的所有信息,包括用户名、性别、年龄、内容等等。
10个步骤实现项目功能,下面开始实例讲解:
1导入模块
import re
import urllibrequest
from bs4 import BeautifulSoup
2添加头文件,防止爬取过程被拒绝链接
def qiuShi(url,page):
################### 模拟成高仿度浏览器的行为 ##############
heads ={
'Connection':'keep-alive',
'Accept-Language':'zh-CN,zh;q=09',
'Accept':'text/html,application/xhtml+xml,application/xml;
q=09,image/webp,image/apng, / ;q=08',
'User-Agent':'Mozilla/50 (Windows NT 100; WOW64) AppleWebKit/53736
(KHTML, like Gecko) Chrome/6303239132 Safari/53736',
}
headall = []
for key,value in headsitems():
items = (key,value)
headallappend(items)
opener = urllibrequestbuild_opener()
openeraddheaders = headall
urllibrequestinstall_opener(opener)
data = openeropen(url)read()decode()
################## end ########################################
3创建soup解析器对象
soup = BeautifulSoup(data,'lxml')
x = 0
4开始使用BeautifulSoup4解析器提取用户名信息
############### 获取用户名 ########################
name = []
unames = soupfind_all('h2')
for uname in unames:
nameappend(unameget_text())
#################end#############################
5提取发表的内容信息
############## 发表的内容 #########################
cont = []
data4 = soupfind_all('div',class_='content')
data4 = str(data4)
soup3 = BeautifulSoup(data4,'lxml')
contents = soup3find_all('span')
for content in contents:
contappend(contentget_text())
##############end####################################
6提取搞笑指数
#################搞笑指数##########################
happy = []
data2 = soupfind_all('span',class_="stats-vote")
data2 = str(data2) # 将列表转换成字符串形式才可以使用
soup1 = BeautifulSoup(data2,'lxml')
happynumbers = soup1find_all('i',class_="number")
for happynumber in happynumbers:
happyappend(happynumberget_text())
##################end#############################
7提取评论数
############## 评论数 ############################
comm = []
data3 = soupfind_all('a',class_='qiushi_comments')
data3 = str(data3)
soup2 = BeautifulSoup(data3,'lxml')
comments = soup2find_all('i',class_="number")
for comment in comments:
commappend(commentget_text())
############end#####################################
8使用正则表达式提取性别和年龄
######## 获取性别和年龄 ##########################
pattern1 = '<div class="articleGender (w )Icon">(d )</div>'
sexages = recompile(pattern1)findall(data)
9设置用户所有信息输出的格局设置
################## 批量输出用户的所以个人信息 #################
print()
for sexage in sexages:
sa = sexage
print(' ' 17, '= = 第', page, '页-第', str(x+1) + '个用户 = = ',' ' 17)
print('用户名:',name[x],end='')
print('性别:',sa[0],' 年龄:',sa[1])
print('内容:',cont[x])
print('搞笑指数:',happy[x],' 评论数:',comm[x])
print(' ' 25,' 三八分割线 ',' ' 25)
x += 1
###################end##########################
10设置循环遍历爬取13页的用户信息
for i in range(1,14):
url = ' >
简单的这种查找即用“in”指令就可以。
例如你要搜索的是一个文本,那么可以用readlin()逐行读取文本
然后对每一个line进行判断:
if 'think' in line:
print(line)
当然,如果进行更复杂的匹配就要用正则表达式了
如果不会的话可以去网上找找教程,我没法给你说,因为光正则表达式本身的教程就是可以出一本书的……
以上就是关于python 列表a中的5个元素由用户输入获得,print出列表a全部的内容,包括:python 列表a中的5个元素由用户输入获得,print出列表a、python用re.findall获取网页全部符合要求的元素、如何在python中取出所有符合条件的行标,如一行相加等于5的所有对应行标。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)