提示:本学习来自Ehco前辈的文章, 经过实现得出的笔记。

目标网站

http://dianying.2345.com/top/

网站结构

要爬的部分,在ul标签下(包括li标签), 大致来说迭代li标签的内容输出即可。

遇到的问题?

代码简单, 但遇到的问题很多。

一: 编码

这里统一使用gbk了。

二: 库

过程中缺少requests,bs4,idna,certifi,chardet,urllib3等库, 需要手动添加库, 我说一下我的方法

库的添加方法:

例如:urllib3

百度urllib3,通过链接下载到本地

我下载第一个

解压把urllib3文件夹扔进python安装目录的Lib目录下即可

三: 下载图片链接

这个就有意思了, 之前我是这样写的

f.write(requests.get(img_url).content)

报错

File "C:\Users\Shinelon\AppData\Local\Programs\Python\Python36\lib\requests\models.py", line 379, in prepare_url
raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL '//imgwx5.2345.com/dypcimg/img/c/65/sup196183_223x310.jpg': No schema supplied. Perhaps you meant http:////imgwx5.2345.com/dypcimg/img/c/65/sup196183_223x310.jpg? Process finished with exit code 1

图片是这样的,也无法进行迭代输出下载

没办法,后来自己自动给链接加上http:

img_url2 = 'http:' + img_url
f.write(requests.get(img_url2).content)
print(img_url2)
f.close()

然后就正常了。

附上代码

import requests
import bs4 def get_html(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status
r.encoding = 'gbk'
return r.text
except:
return "someting wrong" def get_content(url):
html = get_html(url)
soup = bs4.BeautifulSoup(html, 'lxml') movieslist = soup.find('ul', class_='picList clearfix')
movies = movieslist.find_all('li') for top in movies:
#爬取图片src
img_url = top.find('img')['src']
#爬取影片name
name = top.find('span', class_='sTit').a.text
try:
#爬取影片上映时间
time = top.find('span', class_='sIntro').text
except:
time = "暂无上映时间"
#爬取电影角色主演
actors = top.find('p', class_='pActor')
actor = ''
for act in actors.contents:
actor = actor + act.string + ' '
#爬取电影简介
intro = top.find('p', class_='pTxt pIntroShow').text
print("片名:{}\t{}\n{}\n{} \n \n ".format(name, time, actor,intro))
#下载图片到指定目录
with open('/Users/Shinelon/Desktop/1212/'+name+'.png','wb+') as f:
img_url2 = 'http:' + img_url
f.write(requests.get(img_url2).content)
print(img_url2)
f.close() def main():
url = 'http://dianying.2345.com/top/'
get_content(url) if __name__ == "__main__":
main()

结果

零基础Python爬虫实现(爬取最新电影排行)的更多相关文章

  1. Python爬虫项目--爬取猫眼电影Top100榜

    本次抓取猫眼电影Top100榜所用到的知识点: 1. python requests库 2. 正则表达式 3. csv模块 4. 多进程 正文 目标站点分析 通过对目标站点的分析, 来确定网页结构,  ...

  2. Python爬虫之爬取慕课网课程评分

    BS是什么? BeautifulSoup是一个基于标签的文本解析工具.可以根据标签提取想要的内容,很适合处理html和xml这类语言文本.如果你希望了解更多关于BS的介绍和用法,请看Beautiful ...

  3. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  4. from appium import webdriver 使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium)

    使用python爬虫,批量爬取抖音app视频(requests+Fiddler+appium) - 北平吴彦祖 - 博客园 https://www.cnblogs.com/stevenshushu/p ...

  5. Python爬虫之爬取站内所有图片

    title date tags layut Python爬虫之爬取站内所有图片 2018-10-07 Python post 目标是 http://www.5442.com/meinv/ 如需在非li ...

  6. python爬虫实战---爬取大众点评评论

    python爬虫实战—爬取大众点评评论(加密字体) 1.首先打开一个店铺找到评论 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经 ...

  7. Python爬虫之爬取淘女郎照片示例详解

    这篇文章主要介绍了Python爬虫之爬取淘女郎照片示例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 本篇目标 抓取淘宝MM ...

  8. 第一个nodejs爬虫:爬取豆瓣电影图片

    第一个nodejs爬虫:爬取豆瓣电影图片存入本地: 首先在命令行下 npm install request cheerio express -save; 代码: var http = require( ...

  9. 爬取豆瓣电影排行top250

    功能描述V1.0: 爬取豆瓣电影排行top250 功能分析: 使用的库 1.time 2.json 3.requests 4.BuautifulSoup 5.RequestException 上机实验 ...

随机推荐

  1. CI持续集成系统环境--Gitlab+Gerrit+Jenkins完整对接

    原文地址https://www.cnblogs.com/kevingrace/p/5651447.html 近年来,由于开源项目.社区的活跃热度大增,进而引来持续集成(CI)系统的诞生,也越发的听到更 ...

  2. vue中输入框聚焦,自动跳转下一个输入框

    比如 点击入库,el-dialog弹出来,然后自动聚焦第一个输入框,当输入框有值的时候,自动跳转下一个输入框 这个需求 直接上菜: this.$refs.lbj.focus()其实直接这么写也可以,但 ...

  3. ping主机不通邮件报警

    #!/bin/bash#Author: wang#Date: 20170825 for ip in "192.168.101.28" do ping -c 1 $ip && ...

  4. Javascript-双色球

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. 关于hibernate总是报错 配置factory的id找不到,mapping配置文件Could not parse mapping document from input stream

    Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream ...

  6. vue--监听属性完成大小写字母间的转换

    监听属性 watch侦听属性的作用是侦听某属性值的变化,从而做相应的操作,侦听属性是一个对象,它的键是要监听的对象或者变量,值一般是函数,当你侦听的元素发生变化时,需要执行的函数,这个函数有两个形参, ...

  7. Some Useful Resources for the Future Usage

    并发编程 http://ifeve.com/ 美国各州 http://114.xixik.com/usa-stats/ 美国各州邮编zip code -> https://www.douban. ...

  8. QT获取窗口句柄

    winId()函数 SendMessage((HWND)(this->dlg->winId()),WM_SEND_MY_MESSAGE,0,0);

  9. 3.GDScript(1)概览

    GDScript 是上面提到的用于Godot的主要语言.和其他语言相比,它与Godot高度整合,有许多优点: 简单,优雅,设计上为Lua.Python.Squirrel等语言用户所熟悉. 加载和编译速 ...

  10. html5 随机数函数

    function selec(low,high){var ch=high-low+1;return Math.floor(Math.random()*ch+low);}for (var i = 0; ...