zoukankan      html  css  js  c++  java
  • Python题目4:爬取电影

    import re # 正则表达式,用于提取数据
    import requests  # 下载网页源代码
    '''
    安装requests模块:pip install requests
    参考文档:https://www.cnblogs.com/jamespan23/p/5526311.html
    '''
    for m in range(1,5):
        url = 'http://www.dytt8.net/html/gndy/dyzz/list_23_'+str(m)+'.html'
        html = requests.get(url) #  使用静态网页
        html.encoding = 'GB2312' # 指定编码格式,通过查看网页源代码得知
        data = re.findall('<a href="(.*?)" class="ulink">',html.text)
        # 提取信息,返回列表
        # (.*?)匹配以'<a href='开头,以'class="ulink"'结尾的任何信息
    
        for n in data:
            url2 = 'http://www.dytt8.net' + n
            html2 = requests.get(url2)
            html2.encoding = 'GB2312'
            ftp = re.findall('<a href="(.*?)">.*?</a></td>',html2.text)
            try:
                with open(r'F:pythonmov.txt','a',encoding='UTF-8') as f:
                # utf-8有可能兼容,不兼容的话使用gb2312
                    f.write(ftp[0]+'
    ') # ftp提取的都是列表,列表不能写入文件,所以要加[0]
            except:
                print('这一页不能下载')
    

      

  • 相关阅读:
    tp5后台开发某些心得
    some note
    py数据抓取小案例(博客
    vue axios的使用
    C# WebApi POST 提交
    WebApi 返回Json
    C# WebApi 处理Area路径问题
    时间比较
    将多行数据合并为一列
    汉字转换拼音
  • 原文地址:https://www.cnblogs.com/rouge2017/p/8353623.html
Copyright © 2011-2022 走看看