Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过div中的表进行解析_Python_Web Scraping_Beautifulsoup_Findall - Fatal编程技术网

Python 如何通过div中的表进行解析

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

我知道我们可以使用get_text获取值,并使用for循环遍历行。但是我无法仅查找表:(

唯一的区别是find_all()返回一个包含单个结果的列表,而find()只返回结果

如果find_all()找不到任何内容,则返回空列表。如果find()找不到任何内容,则返回None:

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')