python学习之re例子

版权声明: https://blog.csdn.net/qq_25233621/article/details/81140151
import re
import requests
from bs4 import BeautifulSoup
import lxml

def getHtml():#获取网页信息
    url="http://www.maomitt9.com/"#网页链接
    Bs={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'}
    r=requests.get(url,headers=Bs,timeout=60)
    print(r.status_code)
    r.raise_for_status()#正确访问
    r.encoding=r.apparent_encoding#转码
    #soup=BeautifulSoup(r.text,'lxml')
    #print(soup.prettify())
    #return soup
    return r.text

def anylizeHtml(soup):#简单爬取特定信息
    # massageslist=[]
     urls=re.compile(r'\"/\w{1,5}/.{1,8}/\d{5}.html\"',re.S)#查找网页链接
     names=re.compile(r'class=\"s1\">.{1,20}</span>',re.S)#电影名
     print(re.findall(urls,soup))
     print(re.findall(names,soup))
     

if __name__ == '__main__':
    text=getHtml()
    anylizeHtml(text)
    

猜你喜欢

转载自blog.csdn.net/qq_25233621/article/details/81140151