首页 技术 正文
技术 2022年11月18日
0 收藏 828 点赞 2,178 浏览 1327 个字

入门第一个爬虫一般都是爬这个,实在是太简单。用了 requests 和 bs4 库。

1、检查网页元素,提取所需要的信息并保存。这个用 bs4 就可以,前面的文章中已经有详细的用法阐述。

2、找到下一个 url 地址。本例中有两种方法,一是通过 url 的规则,本例中通过比较发现,只要更改 url 中的 start 参数值就可以;二是通过下一个页的标签获取下一页的 url。代码中采用了第一种方法。

3、判断退出条件,爬虫不可能无限制循环下去。

在这个最简单的示例中,实现以上三步一个爬虫就完成了。简单到不想做其他说明,直接看代码吧。

"""
爬取豆瓣电影Top250
"""import os
import re
import time
import requests
from bs4 import BeautifulSoupdef download(url, page):
print(f"正在爬取:{url}")
html = requests.get(url).text # 这里不加text返回<Response [200]>
soup = BeautifulSoup(html, 'html.parser')
lis = soup.select("ol li")
for li in lis:
index = li.find('em').text
title = li.find('span', class_='title').text
rating = li.find('span', class_='rating_num').text
strInfo = re.search("(?<=<br/>).*?(?=<)", str(li.select_one(".bd p")), re.S | re.M).group().strip()
infos = strInfo.split('/')
year = infos[0].strip()
area = infos[1].strip()
type = infos[2].strip()
write_fo_file(index, title, rating, year, area, type)
page += 25
if page < 250:
time.sleep(2)
download(f"https://movie.douban.com/top250?start={page}&filter=", page)def write_fo_file(index, title, rating, year, area, type):
f = open('movie_top250.csv', 'a')
f.write(f'{index},{title},{rating},{year},{area},{type}\n')
f.closeddef main():
if os.path.exists('movie_top250.csv'):
os.remove('movie_top250.csv') url = 'https://movie.douban.com/top250'
download(url, 0)
print("爬取完毕。")if __name__ == '__main__':
main()

相关博文推荐:

Python爬虫实例:爬取猫眼电影——破解字体反爬

Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919