鱼C论坛

 找回密码
 立即注册
查看: 1004|回复: 7

为啥这段代码爬出来的是空白

[复制链接]
发表于 2024-11-5 22:52:19 | 显示全部楼层 |阅读模式

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

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

x
import requests
from bs4 import BeautifulSoup

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}

# 首先我们写好抓取网页的函数
def get_html(url):
    try:
        r = requests.get(url,headers=headers, timeout=30)
        r.raise_for_status()
        #这里我们知道百度贴吧的编码是utf-8,所以手动设置的。爬去其他的页面时建议使用:
        #r.endcodding = r.apparent_endconding
        r.encoding = 'utf-8'
        return r.text
    except:
        return " ERROR "


def get_content(url):
    """
        分析贴吧的网页文件,整理信息,保存在列表变量中
    """
    # 初始化一个列表来保存所有的帖子信息:
    comments = []
    # 首先,我们把需要爬取信息的网页下载到本地
    html = get_html(url)
    # 我们来做一锅汤
    soup = BeautifulSoup(html, 'html.parser')
    # print(soup.prettify())
    # 按照之前的分析,我们找到所有具有‘ j_thread_list clearfix’属性的li标签。返回一个列表类型。
    liTags = soup.find_all('li', attrs={'class': ' j_thread_list clearfix thread_item_box'})
    print('litags:',liTags)
    for li in liTags:
        comment = {}
        try:
            comment['title'] = li.find('a', attrs={'class': 'j_th_tit '}).text.strip()
            comment['link'] = "http://tieba.baidu.com/" + li.find('a', attrs={'class': 'j_th_tit '})['href']
            comment['name'] = li.find('a', attrs={'class': 'frs-author-name j_user_card '}).text.strip()
            comment['time'] = li.find('span', attrs={'class': 'threadlist_reply_date pull_right j_reply_data'}).text.strip()
            comment['replyNum'] = li.find('span', attrs={'class': 'threadlist_rep_num center_text'}).text.strip()
            comments.append(comment)
        except:
            print("bad wrong")
    return comments


def Out2File(dict):
    """
    将爬取到的文件写入到本地
    保存到当前目录的 TTBT.txt文件中。
    """
    with open('TTBT.txt', 'w', encoding='utf-8') as f:
        for comment in dict:
            f.write('标题: {} \t 链接:{} \t 发帖人:{} \t 发帖时间:{} \t 回复数量: {} \n'.format(
                comment['title'], comment['link'], comment['name'], comment['time'], comment['replyNum']))
        print('当前页面爬取完成')

def main(base_url, deep):
    url_list = []
    # 将所有需要爬去的url存入列表
    for i in range(0, deep):
        url_list.append(base_url + '&pn=' + str(50 * i))
    print('所有的网页已经下载到本地! 开始筛选信息。。。。')

    #循环写入所有的数据
    for url in url_list:
        content = get_content(url)
        Out2File(content)
    print('所有的信息都已经保存完毕!')


base_url = 'https://tieba.baidu.com/f?kw=python&ie=utf-8'
# 设置需要爬取的页码数量
deep = 3

if __name__ == '__main__':
    main(base_url, deep)

liTags 为啥是空白的啊??

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

使用道具 举报

 楼主| 发表于 2024-11-6 21:06:17 | 显示全部楼层
不对吧  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-7 21:42:30 | 显示全部楼层
现在人工的都没有了吗  都是机器人了???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-11-19 23:15:34 | 显示全部楼层
都回答不是我像要的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-22 06:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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