for tr in soup.find('tbody').children:
AttributeError: 'NoneType' object has no attribute 'children'
getHTMLText(url):#爬取网站数据
try:
r = requests.get(url, timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return '爬取失败'
def fillUnivlist(ulist,html):#解析网站数据
soup = BeautifulSoup(html,"html.parser")
for tr in soup.find('tbody').children:
if isinstance(tr,bs4.element.Tag):
tds = tr('td')
ulist.append([tds[0].text,tds[1].text,tds[2].text,tds[3].text,tds[4].text,tds[5].text,tds[6].text,tds[7].text])
uinfo1 =[]
url1 = 'https://www.chyxx.com/industry/202105/953391.html'
html1 = getHTMLText(url1)
fillUnivlist(uinfo1,html1)
writeUlistfile(uinfo1,'各种油产量初.csv')
运行结果及报错内容
我的解答思路和尝试过的方法
尝试将https改成http无效,text改成string无效
我想要达到的结果
解决报错,run起来