rompin 发表于 2020-4-6 16:23:50

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

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_='pbuttonasciifont').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)

wp231957 发表于 2020-4-6 16:28:13

一级一级的查找,先看each的上一级result是否有数据

zltzlt 发表于 2020-4-6 17:05:32

你忘记返回和调用函数了

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)
    return jishu    # 加上返回
    # 找出一共多少个页面


def find_depth(res):
    soup = bs4.BeautifulSoup(res.text, 'html.parser')
    depth = soup.find('a', class_='pbuttonasciifont').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)


main()    # 调用函数

suchocolate 发表于 2020-4-6 17:25:58

这句:
depth = soup.find('li',class_='pbuttonasciifont').previous_sibling.previous_sibling.text
我看了那个网站,li没有这个class,是下面的a有class。
另外这个语句应该是不对的,应该改一下,你是想拿什么数据?
页: [1]
查看完整版本: 待解决(为什么txt不出来,没有报错)