鱼C论坛

 找回密码
 立即注册
查看: 1478|回复: 2

第二版书上爬虫的问题

[复制链接]
发表于 2019-10-23 15:25:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么报错了说是变量未定义      NameError: name 'depth' is not defined


# p14_2.py
import requests
import bs4
import re

def open_url(url):
    # 使用代理
    # proxies = {"http":"127.0.0.1:1080","https":"127.0.0.1:1080"}
    headers = {'user-agent':'Mozilla/5.0 (windows NT 10.0; WOW64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/57.0.298.98 Safari/537.36'}
    # res = requests.get(url,headers=headers,proxies=proxies)
    return res

def find_movies(res):
    soup = bs4.BeautifulSoup(res.text,'html.parser')
    #电影名
    movies = []
    targets = soup.find_all("div",class_="hd")
    for each in tergets:
        movies.append(each.a.span.text)
    #评分
    ranks = []
    targets = soup.find_all("span",class_="rating_num")
    for each in targets:
        ranks.append('评分: %s ' % each.text)
    #资料
    messages = []
    targets = soup.find_all("div",class_="bd")
    for each in targets:
        try:
            messages.append(each.p.text.split('\n')[1].strip()+each.p.text.split('\n')[2].strip())
        except:
            continue

    result = []
    length = len(movies)
    for i in range(length):
        result.append(movies[i] + ranks[i] + messages[i] + '\n')
    return result

    #找出一共有多少个页面
def find_depth(res):
    soup = bs4.BeautifulSoup(res.text,'html.parser')
    depth = soup.find('span',class_='next').previous_sibling.previous_sibling.text
    return int(depth)


def main():
    host = "https://movie.douban.com/top250"
    res = open_url(host)
    depth = find_depth(res)
    result = []
   
for i in range(depth):
    ulr = host + '/?start=' + str(25 + 1)
    res = open_url(url)
    result.extend(find_movies(res))

with open("豆瓣 TOP250 电影.txt","w",encoding="utf-8") as f:
    for each in result:
        f.write(each)

if __name__ == "__main__":
    main()



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-23 18:40:53 From FishC Mobile | 显示全部楼层
你的depth定义是在main函数下的,最后for循环引用不了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-23 19:50:20 | 显示全部楼层
应该把 for i in range(depth): 以及后面的内容增加一个缩进,同时 ulr 要改成 url

  1. # p14_2.py
  2. import requests
  3. import bs4
  4. import re

  5. def open_url(url):
  6.     # 使用代理
  7.     # proxies = {"http":"127.0.0.1:1080","https":"127.0.0.1:1080"}
  8.     headers = {'user-agent':'Mozilla/5.0 (windows NT 10.0; WOW64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/57.0.298.98 Safari/537.36'}
  9.     # res = requests.get(url,headers=headers,proxies=proxies)
  10.     return res

  11. def find_movies(res):
  12.     soup = bs4.BeautifulSoup(res.text,'html.parser')
  13.     #电影名
  14.     movies = []
  15.     targets = soup.find_all("div",class_="hd")
  16.     for each in tergets:
  17.         movies.append(each.a.span.text)
  18.     #评分
  19.     ranks = []
  20.     targets = soup.find_all("span",class_="rating_num")
  21.     for each in targets:
  22.         ranks.append('评分: %s ' % each.text)
  23.     #资料
  24.     messages = []
  25.     targets = soup.find_all("div",class_="bd")
  26.     for each in targets:
  27.         try:
  28.             messages.append(each.p.text.split('\n')[1].strip()+each.p.text.split('\n')[2].strip())
  29.         except:
  30.             continue

  31.     result = []
  32.     length = len(movies)
  33.     for i in range(length):
  34.         result.append(movies[i] + ranks[i] + messages[i] + '\n')
  35.     return result

  36.     #找出一共有多少个页面
  37. def find_depth(res):
  38.     soup = bs4.BeautifulSoup(res.text,'html.parser')
  39.     depth = soup.find('span',class_='next').previous_sibling.previous_sibling.text
  40.     return int(depth)


  41. def main():
  42.     host = "https://movie.douban.com/top250"
  43.     res = open_url(host)
  44.     depth = find_depth(res)
  45.     result = []
  46.    
  47.     for i in range(depth):
  48.         url = host + '/?start=' + str(25 + 1)
  49.         res = open_url(url)
  50.         result.extend(find_movies(res))

  51.     with open("豆瓣 TOP250 电影.txt","w",encoding="utf-8") as f:
  52.         for each in result:
  53.             f.write(each)

  54. if __name__ == "__main__":
  55.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-20 06:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表