当我找到值时,如何停止网络抓取数据?
How can I stop web-scraping dataes, when i find the value?
我有这个代码,我可以在 Python 2.7 的网站上搜索所有名为 "ctable" 的 table。但我想阻止它,当它到达这个 "ctable" 的值 XXXX 时。我需要直到这个值 XXXX。因此,如果它找到了这段文字,我想停止网络抓取这些 tables.
可能吗?
这是我的代码:
soup = BeautifulSoup(x, 'lxml')
datatable=[]
for ctable in soup.find_all('table', "ctable" )[:-1]:
for record in ctable.find_all('tr'):
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
datatable.append(temp_data)
我试过这个:
datatable=[]
for ctable in soup.find_all('table', "ctable" )[:-1]:
for record in ctable.find_all('tr'):
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
if 'modul' in data.text:
break
datatable.append(temp_data)
在您的代码中实现一个 break
运算符:
...
(your code above)
datatable=[]
stop = 0
for ctable in soup.find_all('table', "ctable" )[:-1]:
if stop == 1:
break
for record in ctable.find_all('tr'):
if stop == 1:
break
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
if 'modul' in data.text:
stop = 1
break
datatable.append(temp_data)
我没有足够注意你有一个三重 for
循环。也许现在它会起作用?
我在每个循环中添加了 break
。
备选 if
和 break
:
datatable=[]
stop = 0
for ctable in soup.find_all('table', "ctable" )[:-1]:
for record in ctable.find_all('tr'):
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
if 'modul' in data.text:
stop = 1
break
datatable.append(temp_data)
if stop == 1:
break
if stop == 1:
break
我有这个代码,我可以在 Python 2.7 的网站上搜索所有名为 "ctable" 的 table。但我想阻止它,当它到达这个 "ctable" 的值 XXXX 时。我需要直到这个值 XXXX。因此,如果它找到了这段文字,我想停止网络抓取这些 tables.
可能吗?
这是我的代码:
soup = BeautifulSoup(x, 'lxml')
datatable=[]
for ctable in soup.find_all('table', "ctable" )[:-1]:
for record in ctable.find_all('tr'):
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
datatable.append(temp_data)
我试过这个:
datatable=[]
for ctable in soup.find_all('table', "ctable" )[:-1]:
for record in ctable.find_all('tr'):
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
if 'modul' in data.text:
break
datatable.append(temp_data)
在您的代码中实现一个 break
运算符:
...
(your code above)
datatable=[]
stop = 0
for ctable in soup.find_all('table', "ctable" )[:-1]:
if stop == 1:
break
for record in ctable.find_all('tr'):
if stop == 1:
break
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
if 'modul' in data.text:
stop = 1
break
datatable.append(temp_data)
我没有足够注意你有一个三重 for
循环。也许现在它会起作用?
我在每个循环中添加了 break
。
备选 if
和 break
:
datatable=[]
stop = 0
for ctable in soup.find_all('table', "ctable" )[:-1]:
for record in ctable.find_all('tr'):
temp_data = []
for data in record.find_all('td'):
temp_data.append(data.text.encode('latin-1'))
if 'modul' in data.text:
stop = 1
break
datatable.append(temp_data)
if stop == 1:
break
if stop == 1:
break