一个简单爬虫

一个简单爬虫–获取豆瓣电影前250部中文名
1、代码如下:

import requests
from bs4 import BeautifulSoup
headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0"
}

url="https://study.scho.com/web/checkpointContent?info=eyJnYW1lSWQiOjg3OTExLCJxdWVzdElkIjoxOTIwMjI0LCJnYW1lVHlwZSI6Im1vZHVsZSJ9"
start_movie=0
while start_movie<250:
    url="https://movie.douban.com/top250?start="+str(start_movie)+"&filter="
    response=requests.get(url,headers=headers)
    content = response.text
    soup =BeautifulSoup(content,"html.parser")
    all_comments = soup.findAll("span",attrs={"class":"title"})
    # tmp_title = ""
    for comments in all_comments:
        if "/" not in comments.string:
            print (comments.string)
        #     tmp_title = comments.string
        # else:
        #     print (tmp_title+comments.string)
        #     tmp_title=""

    start_movie+=25

2、需要安装两个包:

#安装
pip install requests  // 通讯请求
pip install bs4 //处理html
#导入俩包:
import requests
from bs4 import BeautifulSoup

3、遇到代码存在中文报错SyntaxError: Non-ASCII character ‘\xe5’ in file D:\pythonProjection\1_print_demo.py on line 2, but no encoding declared;

修复:在代码第一行输入# coding:UTF-8或者# -- coding:UTF-8 --

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值