鱼C论坛

 找回密码
 立即注册
查看: 818|回复: 3

[已解决]待解决(为什么txt不出来,没有报错)

[复制链接]
发表于 2020-4-6 16:23:50 | 显示全部楼层 |阅读模式

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

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

x
import requests
import bs4
import re


def open_url(url):
    headers={
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400'
    }
    res = requests.get(url,headers=headers)


    return res
def find_animation(res):
    soup = bs4.BeautifulSoup(res.text,'html.parser')
    #动漫名
    animation = []
    targets = soup.find_all("li",class_="anime_icon2")
    for each in targets:
        animation.append(each.h4.a.text)
    #集数
    jishu = []
    targets = soup.find_all("span",class_="anime_icon1_name1")
    for each in targets:
        jishu.append(each.text)

        #找出一共多少个页面
def find_depth(res):
    soup = bs4.BeautifulSoup(res.text,'html.parser')
    depth = soup.find('li',class_='pbutton  asciifont').previous_sibling.previous_sibling.text


    return int(depth)
def main():
    host = "https://www.agefans.tv/recommend"
    res = open_url(host)
    depth = find_depth(res)

    result = []
    #length = len(animation)
    for i in range(depth):
       url = host + '/?start=' + str(25 * i)
       res = open_url(url)
       result.extend(find_animation(res))


    with open("age动漫推荐.txt","w",encoding="utf-8")as f:
        for each in result:
            f.write(each)
最佳答案
2020-4-6 17:25:58
这句:
depth = soup.find('li',class_='pbutton  asciifont').previous_sibling.previous_sibling.text
我看了那个网站,li没有这个class,是下面的a有class。
另外这个语句应该是不对的,应该改一下,你是想拿什么数据?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-6 16:28:13 From FishC Mobile | 显示全部楼层
一级一级的查找,先看each的上一级result是否有数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 17:05:32 | 显示全部楼层
你忘记返回和调用函数了

  1. import requests
  2. import bs4
  3. import re


  4. def open_url(url):
  5.     headers = {
  6.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3741.400 QQBrowser/10.5.3863.400'
  7.     }
  8.     res = requests.get(url, headers=headers)

  9.     return res


  10. def find_animation(res):
  11.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  12.     # 动漫名
  13.     animation = []
  14.     targets = soup.find_all("li", class_="anime_icon2")
  15.     for each in targets:
  16.         animation.append(each.h4.a.text)
  17.     # 集数
  18.     jishu = []
  19.     targets = soup.find_all("span", class_="anime_icon1_name1")
  20.     for each in targets:
  21.         jishu.append(each.text)
  22.     return jishu    # 加上返回
  23.     # 找出一共多少个页面


  24. def find_depth(res):
  25.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  26.     depth = soup.find('a', class_='pbutton  asciifont').previous_sibling.previous_sibling.text

  27.     return int(depth)


  28. def main():
  29.     host = "https://www.agefans.tv/recommend"
  30.     res = open_url(host)
  31.     depth = find_depth(res)

  32.     result = []
  33.     # length = len(animation)
  34.     for i in range(depth):
  35.         url = host + '/?start=' + str(25 * i)
  36.         res = open_url(url)
  37.         result.extend(find_animation(res))

  38.     with open("age动漫推荐.txt", "w", encoding="utf-8")as f:
  39.         for each in result:
  40.             f.write(each)


  41. main()    # 调用函数
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 17:25:58 | 显示全部楼层    本楼为最佳答案   
这句:
depth = soup.find('li',class_='pbutton  asciifont').previous_sibling.previous_sibling.text
我看了那个网站,li没有这个class,是下面的a有class。
另外这个语句应该是不对的,应该改一下,你是想拿什么数据?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 02:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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