爬虫爬取该网站时返回空列表

import requests
from bs4 import BeautifulSoup
import csv

def getHtml(url):
    try:
        r=requests.get(url)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return "fail"
    
def getDATA(ulist,html):
    soup=BeautifulSoup(html,'html.parser')
    table=soup.find_all('tr')
    for row in table:
        cols=[col.text for col in row.find_all('td')]
        if len(cols)==0 or not cols[0].isdigit():
            continue
        ulist.append(cols)

def main():
    unifo=[]
    url='https://www.phb123.com/hangye/qiche/index_1.html'
    html=getHtml(url)
    getDATA(unifo,html)
    print(html)
    print('*'*20)
    print(unifo)
main()

有大佬能帮忙解决一下吗?

第一列不是包含了换行和空白,导致是否数字判断失败,要去掉空白就可以了,加下面加粗的内容,帮助到你可以点击采纳吗,谢谢~~

def getDATA(ulist,html):
    soup=BeautifulSoup(html,'html.parser')
    table=soup.find_all('tr')
    for row in table:
        cols=[col.text for col in row.find_all('td')]
        if len(cols)==0 or not cols[0].strip().isdigit():
           continue
        ulist.append(cols)

给headers加参数。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m