Python 如何通过div中的表进行解析
我知道我们可以使用get_text获取值,并使用for循环遍历行。但是我无法仅查找表:(唯一的区别是find_all()返回一个包含单个结果的列表,而find()只返回结果 如果find_all()找不到任何内容,则返回空列表。如果find()找不到任何内容,则返回None:Python 如何通过div中的表进行解析,python,web-scraping,beautifulsoup,findall,Python,Web Scraping,Beautifulsoup,Findall,我知道我们可以使用get_text获取值,并使用for循环遍历行。但是我无法仅查找表:(唯一的区别是find_all()返回一个包含单个结果的列表,而find()只返回结果 如果find_all()找不到任何内容,则返回空列表。如果find()找不到任何内容,则返回None: html = urlopen("http://www.moneycontrol.com/india/stockpricequote/computers-software/tataconsultancyservices/TC
html = urlopen("http://www.moneycontrol.com/india/stockpricequote/computers-software/tataconsultancyservices/TCS")
bsObj = BeautifulSoup(html, "html.parser")
link = bsObj.findAll("div", id="findet_1")
table1 = link.find('table').find_all('tr')
试试这个:
link = bsObj.findAll("div", id="findet_1")
if link:
table1 = link[0].find('table').find_all('tr')
还是这个
table_div = html.find('div' , {'id': 'findet_1', 'name': 'findet_1' })
table = table_div.find('table')
谢谢。这很有效。我可以知道如何在文本文件中存储每一行吗。使用csv模块
table_div = html.find('div' , {'id': 'findet_1', 'name': 'findet_1' })
table = table_div.find('table')
table_div = html.find('div' , {'id': 'findet_1', 'name': 'findet_1' })
table = table_div.find_all('tr')