正则表达式并不是Python的一部分。正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分强大。
得益于这一点,在提供了正则表达式的语言里,正则表达式的语法都是一样的,区别只在于不同的编程语言实现支持的语法数量不同;但不用担心,不被支持的语法通常是不常用的部分。如果已经在其他语言里使用过正则表达式,只需要简单看一看就可以上手了。
http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
http://deerchao.net/tutorials/regex/regex.htm
http://tool.oschina.net/regex/
import requests
from bs4 import BeautifulSoup
import re
#coding:utf8
class Spider_Moder():
url = 'http://www.qiushibaike.com/hot/'
response = requests.get(url)
response.encoding = 'utf8'
soup = BeautifulSoup(response.text,'lxml')
users = soup.select('div.author.clearfix > a:nth-of-type(1)')
contents = soup.select('a.contentHerf > div > span')
for user,content in zip(users,contents):
title = re.search('alt=\"\S*\"',str(user),flags=0)
article = content.get_text()
print(title.group(0)[5:-1])
print(article)
print('\n')
Spider_Moder