Python爬虫实战--使用python爬取网站数据

1、获取url​:输入想要爬取的网站​url。
2、发送请求:使用python的requests库获取url​并发送请求。
3、提取数据:使用正则表达式提取想要获取的数据​。
4、保存数据:得到想要的数据后,可存放到文件中或数据库。
上代码实例:

import requests
import re
url=”网站Url”
Headers={“user-Agent”:””}
html=Requests.get(url,headers=headers)
print(html)
video_id=re.findall(‘<a href=”(.*?)” class=”vervideo-lilink actplay”>’,html.text)
print(video_id)
video_url=[]
starturl=””  
for vid in video_id:
    newurl=starturl+vid
    #print(newurl)
    video_url.append(newurl)
    #print(video_url)
for purl in video_url:
     html_data=requests.get(purl,headers=headers)
     print(html_data.text)
     playurl=re.findall(‘srcUrl=”(.*?)”,’,html_data.text)[0]
     content=requests.get(playurl,headers=headers).content
     video_name=re.findall(‘<h1 class=”video-tt”>(.*?)</h1>’,html_data.text)[0]
     path=”video/”  #爬取的视频存放地址
     filepath=path+video_name+”.mp4”
     with open(filepath,”wb”) as f:
        f.write(content)
      print(“下载%s视频”%video_name)在这里插入代码片
发布了9 篇原创文章 · 获赞 0 · 访问量 282

猜你喜欢

转载自blog.csdn.net/u013465115/article/details/104364447