zoukankan      html  css  js  c++  java
  • Python爬取腾讯视频电影名称和链接(一)

     1 import requests
     2 import json
     3 from bs4 import BeautifulSoup       #网页解析获取数据
     4 import sys
     5 import re
     6 import urllib.request,urllib.error #制定url,获取网页数据
     7 import sqlite3
     8 import xlwt     #excel操作
     9 
    10 def get_ten():
    11     url="https://v.qq.com/channel/movie?_all=1&channel=movie&listpage=1&sort=18"
    12     headers={
    13         'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '+
    14                        'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'
    15     }
    16     # res = urllib.request.urlopen(url)
    17     res = urllib.request.Request(url=url,headers=headers)       #编辑request请求
    18     response=urllib.request.urlopen(res).read().decode()        #读取
    19     html=BeautifulSoup(response,"html.parser")      #解析
    20     #
    21     # list=html.select(".figure_score")
    22     # for item in list:
    23     #     print(item)
    24     dataRes=[]
    25     findLink=re.compile(r'href="(.*?)"')        #链接
    26     findName=re.compile(r'title="(.*?)"')       #影片名
    27     soup=html.find_all(r"a",class_="figure")
    28     for i in soup:
    29         # print(i)
    30         words=str(i)
    31         dataRes.append(re.findall(findLink,words))       #添加链接
    32         dataRes.append(re.findall(findName,words))       #添加影片名
    33     for i in dataRes:
    34         print(i)
    35     # print(html)
    36     # print(html.head.contents)       #输出tag的所有子节点(list)
    37     # print(response)
    38     return res
    39 if __name__ == '__main__':
    40     get_ten()

  • 相关阅读:
    JavaScript 中正则匹配时结果不一致的问题
    /dev/null
    Xcode 中通过 target 运行多个 c 程序
    Xcode 调试时打印变量值
    Recoil 请求的刷新之使用随机参数
    npm ci 与 install 的区别
    项目中私有 npm 源的设置
    Crontab 的使用
    Nest 在日志中打印对象
    配置 ESLint 自动格式化自闭合标签(Self closing tag)
  • 原文地址:https://www.cnblogs.com/rainbow-1/p/14708814.html
Copyright © 2011-2022 走看看