天天看点

计算机导论学习第一课笔记

第一部分、字符串学习(使用PYTHON)

1、字符串+数字的情况

print 'apple'+'!'*3
           

显示内容:apple!!!

2、索引字符串

#显示内容:t
print 'test'[0]

#显示内容:末尾的‘t’
print 'test'[-1]
           

3、选择字符串的子序列

#显示内容:est
print 'test'[1:]

#不显示任何内容
print 'test'[1:1]

#显示内容:tes
print 'test'[:3]
           
#显示内容:test,与print 'test'结果相同
print 'test'[:]
           

4、查找字符串中的字符串

#显示内容:5
#find函数返回'for'所在位置号5
text = 'test for you'
print text.find('for')

#显示内容:14(第二个“for”所在位置号)
text2 = 'test for you, for me and for us!'
print text2.find('for',6)
           

第二部分、网页的简单抓取

1、网页超链接的特征

<a href="http://XXX.XXXX....." target="_blank" rel="external nofollow" >
           

2、利用字符串的知识从网页 <a href="" target="_blank" rel="external nofollow" >标签中提取超链接信息

page =('<div id="top_bin"><div id="top_content" class="width60">'
'<div class="float-left"><a href="http://test.com" target="_blank" rel="external nofollow" >')
start_link = page.find('<a href=')
start_quote = page.find('"',start_link)
end_quote = page.find('"',start_quote + 1)
url = page[start_quote+1:end_quote]
print url
           

显示url内容为:http://test.com。