美文网首页
python爬取猫眼电影top100

python爬取猫眼电影top100

作者: 485b1aca799e | 来源:发表于2017-06-18 10:44 被阅读0次
    #导入包
    import requests
    from bs4 import BeautifulSoup
    import re
    import pandas as pd
    
    
    file_url="http://maoyan.com/board/4?"+"offset="#猫眼电影top100第一页
    url_setx=["http://maoyan.com/board/4?"]#用于测试
    url_set=["http://maoyan.com/board/4?"]
    
    for i in range(10,100,10):
        url_set.append("http://maoyan.com/board/4?offset="+str(i))
    print(url_set)
    #使用字符串循环连接,得到猫眼top100的十个url连接
    
    name=[]#film name
    star=[]#film star
    date=[]# film date 
    
    for url in url_set:
        html=requests.get(url).content
        x=BeautifulSoup(html)
        
        y=x.find_all(name="a",attrs={"href":re.compile("\/films\/[0-9]{1,6}"),"class":"image-link"})
        print(y)
        for i in y:
            i0=string=re.sub(string=i.attrs['title'],pattern="\n",repl="")
            name.append(i0)
            
        z=x.find_all(name="p",attrs={"class":"star"})
        for i in z:
            i1=re.sub(string=i.string,pattern=u"主演:",repl="")
            i1=re.sub(string=i1,pattern=" *",repl="")
            i1=re.sub(string=i1,pattern="\n",repl="")
            star.append(i1)
        
        p=x.find_all(name="p",attrs={"class":"releasetime"})
        for i in p:
            date.append(re.sub(string=re.sub(string=i.string,pattern=u"上映时间:",repl=""),pattern="\(.*\)",repl=""))
            
    #cbind into a DataFrame
    data={"name":name,"star":star,"date":date}
    x=pd.DataFrame(data)
    print(x)
    
    
    
    

    相关文章

      网友评论

          本文标题:python爬取猫眼电影top100

          本文链接:https://www.haomeiwen.com/subject/tgsiqxtx.html