(一)正则表达式
(二)正则表达式语法:
经典例子:
IP地址:
(三)常用方法:
1,第一个方法:re.search()返回match对象
2,第二个方法:re.match():当匹配的第一个字符不符合,则返回空,返回match对象
3,第三个方法:re.findall()
4,第四个方法:re.split()
5,第五个方法:re.finditer()返回match对象
6,第六个方法:re.sub()
7,第七个方法:re.compile()
8,两种使用re对象的方法的方式:使用面向对象方式时,将正则表达式写入compile()方法中,re对象的方法中就不用正则表达式参数了;
(四)Match对象
1,match对象的属性:
2,match方法:
(五)正则表达式的匹配
1,贪婪匹配
2,最小匹配
例子:
import re rE=re.compile(r"[A-Z].*[A-Z]") ls=rE.search("adaAdssdDsdsFdsdsdM") print(ls.group(0))
截图:
import re rE=re.compile(r"[A-Z].*?[A-Z]") ls=rE.search("adaAdssdDsdsFdsdsdM") print(ls.group(0))
截图:
(六)re库的使用例子
实例一:
1,功能:
2,实现难点:
3,准备工作:
接口:https://s.taobao.com/search?q="搜索的关键词"
分页:第1页:https://s.taobao.com/search?q="搜索的关键词"xxxxxxxxxxxs=0
第2页:https://s.taobao.com/search?q="搜索的关键词"xxxxxxxxxxxs=44
第3页:https://s.taobao.com/search?q="搜索的关键词"xxxxxxxxxxxs=88
商品名称:"raw_title":"uek小学生书包男孩女生1-3-6年级护脊双肩背包6-12岁轻便儿童书包"(存入脚本)
商品价格:"view_price":"198.00"(脚本中)
获取网页请求头的信息方式:
4,实现步骤:
import requests import re #步骤1获取商品页面信息 def getHTMLText(url): kv = {'Cookie':'thw=cn; t=cce1b71dee0c6103ee00c2a80f5d58b8; cookie2=1729efb908bb47c9173e7285b76bcf1f; _tb_token_=37535f9e637ee; _samesite_flag_=true; enc=0MctOlChFlCV5nZPM9I%2BvVj4iB8%2BJrpziCIjwoMf9H8E9LNIgKTgxu8%2BCSUFi9bSBiSB%2FDa5a9LfUco5h2f5%2Bg%3D%3D; JSESSIONID=24C312D5C90EBF0B9899868DCFB187FD; hng=CN%7Czh-CN%7CCNY%7C156; mt=ci=0_0; alitrackid=www.taobao.com; lastalitrackid=www.taobao.com; _uab_collina=158054440702373535211543; isg=BMXFMTj6N3FEpBO8M49bhYzg1wH_gnkUQBjla8cqiPwLXuXQj9O75js8aAKoBZHM; l=cBLFG56PQxE7VxgtBOCw5uI8Lz7tjIOYYuPRwCDMi_5Qw6Ls55bOo4N2tFp6csWdOPTB4eJvIYp9-etkiKy06Pt-g3fP.; cna=7ZWXFhLSeHECAd3As7NcDDt3; v=0; miid=664408812090938164; tk_trace=oTRxOWSBNwn9dPyorMJE%2FoPdY8zfvmw%2Fq5hp3RebXyJYzZUKAW1r6Uz%2FmMSvZbsMK3FmmZCXmyWRkMO1h3bnCN3x3ZrRJ7yYJdsRNoxMHGQpCyrQg3UzMo56H4tZmIQynT2CEisdNFijmGtI6hnUSL2LXCi4gKgBkS8Rgapd4MwIAFja5Js0G49JqnDhbWc1sKw79UmzqcVt7Tw0az0KP0yXz%2B0Li96D%2FooNJq9RYuD5ymYWzhG%2FmPLfTyzOOMEzkaw7usLWvRNcuQeUOw8Aji5KPbwH; x5sec=7b227365617263686170703b32223a223931346262326434363731346639336664383162376333356535323062363562434b377431664546454d4c306c666d38785065696f51456144544d304d5445784d4459344d546b374d54413d227d', 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0','Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding':'gzip, deflate, br','Accept-Language':'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2','Connection':'keep-alive','Host':'s.taobao.com','Referer':'https://s.taobao.com/search/_____tmd_____/verify/?nc_token=95d15a040668aef6de1945952b6d3d6f&nc_session_id=01Zi4sj2c1tpzARQSsI0rq4Cgb5Sg381G1L90GIrfuH6mT-4CLxi8J5_8kfLz3jmo6N8HPWMx6QlMWV-Nw9LKhFln6-0AXmw2z9nEi93BnEERLHjVyYXuOUK6PXRbErcqBWuWG0mV-a-DvE0pTXq6UJzeagecKV3_fbyDJFmMMda9Gn-gwOELXRwI79faMGTxFHvJG0DpdSHZOdyTDPcKc6A&nc_sig=05GpvesdJBVOhMKscmy0iTGVDhDcartZBI8_OhtG395T3MbvF8jsEiyBZ7_89OfHOu1nVfPaE5fquc_8txuX6MuF8JQQQg6KQjDn6GhIUSV4fA9h6a7L0JgEFy3pW-zi6nL8tX3gUMBIC4YgQfgM20A7aweY_FNSUFEk-nEfY_Qwzkpdmguqt_-uH0uG2xqNmbhzIiGJzKxLpS1LnjuS6F8jnEPs_3D0-lJiwaplLuVDZq1_zEMGxVwW_8z-7wUDFCWI9DTT7KPOs-J3qa5Yebuw4i27GuhH6ngF7lLWFCfZj6_1L4lvqAx25LM2Tin_MGdXxH5yNP9qr_zDPI2kztLEB0VESqjnwGcZ4Ovek3EkEo2o3GOxbowBqfBsLfhdzrp7i1nHL_dhh2gabB_Lino1sAxQHojeOF39MSL4bxBD5Ffwge4v4hLjRYG86tawsBHxVCueuVDLHjdNsABRbmWC-ezyLl12sTvnDX542Pzls&x5secdata=5e0c8e1365474455070961b803bd560607b52cabf5960afff39b64ce58073f78f68ede033dd239842063c29628191423866f9620b863c667132a90ce579d5cd75c7327bde2bcde85def97069291fa34dbdd96b66a9dff8da5f07d2fd13ea072445cb0ce36cef9f62cbd52852a03cf8ba461ee819ca12264cfd380e1ff9a3181721bc44fcd5aa925118d721cd93646f3e566899a389acd33f04add41433f5dd657f7806228d1f17d85334904897a5fa2e79a52a883b8d79c21b1904b01749f64ff68ede033dd239842063c29628191423f26a33fc19185ca7f5ba435d1801cd576b8357c40b8852e10bee2dd322fdfa01b85d13ca384528f05b373d3a77a70575ad921bb1d36afdc5973c0455682491a957f7918a4f2572499cc398910575bb4ae5b2a48d9c0185c8d8521d59b4860b9243a2952e026506275152d2dce642e18a4440bf0b3e57db00024c36b841c1cc35ed81c65bebf3b9df46dd6afed6f199892c38573d94a1e033206e485398b2371f6578a595e91f44da415c8660f5f2584e2dcd04435273e80c8ba41a4b44ca79f946d7c07b418bd61930fcc7f43085f215602ff14c1eeffa993259bf8351d819eb3f4129c5e95a897ae925e3fabb3e1a8cfc76271052aec7cfa7d67310728cc6e8&x5step=100&nc_app_key=X82Y__2ec484ab2f20befbd6f0aadd26b8bc5b' ,'TE':'Trailers','Upgrade-Insecure-Requests':'1'} #设置访问的请求头,否则会被反爬虫限制登录或验证等 try: r = requests.get(url, headers=kv,timeout=30) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: return "" #步骤2分析页面内容 def parsePage(ilt, html): try: rEo=re.compile(r'\"view_price\"\:\"[\d\.]*\"') plt=rEo.findall(html) #价格 rEt=re.compile(r'\"raw_title\"\:\".*?\"') tlt=rEt.findall(html) #商品名称 for i in range(len(plt)): #eval()方法去掉双引号,将内容通过:分割 price = eval(plt[i].split(':')[1]) title = eval(tlt[i].split(':')[1]) ilt.append([price, title]) except: print("") #步骤3打印商品信息 def printGoodsList(ilt): tplt = "{0:^4}\t{1:^8}\t{2:{3}^25}" print(tplt.format("序号", "价格", "商品名称",chr(12288))) print("-"*90) count = 0 for g in ilt: count = count + 1 print(tplt.format(count, g[0], g[1],chr(12288))) print("-"*90) #主函数 def main(): goods = '书包' #搜索商品关键词 depth = 3 #设置爬取页面数 start_url = 'https://s.taobao.com/search?q=' + goods infoList = [] for i in range(depth): try: url = start_url + '&s=' + str(44 * i) #第i+1页的URL html = getHTMLText(url) parsePage(infoList, html) except: continue #当某一页爬取错误,则结束继续爬取下一页, printGoodsList(infoList) #程序入口 if __name__=="__main__": main()
截图:
实例二:
1,功能:
2,实现难点:
3,准备工作:网站选取原则:
爬取链接:https://quote.stockstar.com/stock/sha_3_1_x.html 股票综合排名(x从1到53页)
http://q.stock.sohu.com/cn/xxxxxx/index.shtml 搜狐个股股票信息
先爬取东方财富的股票综合排名:获取股票代码,放入搜狐股票的链接中转到个股信息
4,步骤: